which describes how to perform zipping with C#.
My application is in VB.NET so I am trying to translate a class thatperforms the zipping into VB code. I have managed to translate theclass except for two lines. The original C# code is as follows. Mytranslated VB code with errors in emphasized red color can be found just beneath the C#code.The error message I get for the two lines are:
"c:\inetpub\wwwroot\TestApplication\Zip.vb(81):'TestApplication.VbZip.EnumerationMethod' is a delegate type. Delegateconstruction permits only a single AddressOf expression as an argumentlist. Often an AddressOf expression can be used instead of a delegateconstruction.".
Can anyone help me translating these two lines?
using System;
using System.Collections;
using java.util;
using java.util.zip;
namespace CsZip
{
public delegate Enumeration EnumerationMethod();
/// <summary>
/// Wraps java enumerators
/// </summary>
public class EnumerationAdapter : IEnumerable
{
private class EnumerationWrapper : IEnumerator
{
private EnumerationMethod m_Method;
private Enumeration m_Wrapped;
private object m_Current;
public EnumerationWrapper(EnumerationMethod method)
{
m_Method = method;
}
// IEnumerator
public object Current
{
get { return m_Current; }
}
public void Reset()
{
m_Wrapped = m_Method();
if (m_Wrapped == null)
throw newInvalidOperationException();
}
public bool MoveNext()
{
if (m_Wrapped == null)
Reset();
bool Result = m_Wrapped.hasMoreElements();
if (Result)
m_Current = m_Wrapped.nextElement();
return Result;
}
}
private EnumerationMethod m_Method;
public EnumerationAdapter(EnumerationMethod method)
{
if (method == null)
throw new ArgumentException();
m_Method = method;
}
// IEnumerable
public IEnumerator GetEnumerator()
{
return new EnumerationWrapper(m_Method);
}
}
public delegate bool FilterEntryMethod(ZipEntry e);
/// <summary>
/// Zip stream utils
/// </summary>
public class ZipUtils
{
public static void CopyStream(java.io.InputStream from, java.io.OutputStream to)
{
sbyte[] buffer = new sbyte[8192];
int got;
while ((got = from.read(buffer, 0, buffer.Length)) > 0)
to.write(buffer, 0, got);
}
public static void ExtractZipFile(ZipFile file, string path, FilterEntryMethod filter)
{
foreach(ZipEntry entry in new EnumerationAdapter(newEnumerationMethod(file.entries)))
{
if (!entry.isDirectory())
{
if ((filter == null ||filter(entry)))
{
java.io.InputStream s = file.getInputStream(entry);
try
{
string fname =System.IO.Path.GetFileName(entry.getName());
string newpath = System.IO.Path.Combine(path,System.IO.Path.GetDirectoryName(entry.getName()));
System.IO.Directory.CreateDirectory(newpath);
java.io.FileOutputStream dest = newjava.io.FileOutputStream(System.IO.Path.Combine(newpath, fname));
try
{
CopyStream(s, dest);
}
finally
{
dest.close();
}
}
finally
{
s.close();
}
}
}
}
}
public static ZipFile CreateEmptyZipFile(string fileName)
{
new ZipOutputStream(new java.io.FileOutputStream(fileName)).close();
return new ZipFile(fileName);
}
public static ZipFileUpdateZipFile(ZipFile file, FilterEntryMethod filter, string[] newFiles)
{
string prev = file.getName();
string tmp = System.IO.Path.GetTempFileName();
ZipOutputStream to = new ZipOutputStream(newjava.io.FileOutputStream(tmp));
try
{
CopyEntries(file, to, filter);
// add entries here
if (newFiles != null)
{
foreach(string f in newFiles)
{
ZipEntry z =new ZipEntry(f.Remove(0, System.IO.Path.GetPathRoot(f).Length));
z.setMethod(ZipEntry.DEFLATED);
to.putNextEntry(z);
try
{
java.io.FileInputStream s = newjava.io.FileInputStream(f);
try
{
CopyStream(s, to);
}
finally
{
s.close();
}
}
finally
{
to.closeEntry();
}
}
}
}
finally
{
to.close();
}
file.close();
// now replace the old file with the new one
System.IO.File.Copy(tmp, prev, true);
System.IO.File.Delete(tmp);
return new ZipFile(prev);
}
public static void CopyEntries(ZipFile from, ZipOutputStream to)
{
CopyEntries(from, to, null);
}
public static void CopyEntries(ZipFile from, ZipOutputStream to, FilterEntryMethod filter)
{
foreach(ZipEntry entry in new EnumerationAdapter(newEnumerationMethod(from.entries)))
{
if (filter == null || filter(entry))
{
java.io.InputStream s =from.getInputStream(entry);
try
{
to.putNextEntry(entry);
try
{
CopyStream(s, to);
}
finally
{
to.closeEntry();
}
}
finally
{
s.close();
}
}
}
}
}
}
VB.NET code:
Imports System
Imports System.Collections
Imports java.util
Imports java.util.zip
Namespace VbZip
Public Delegate Function EnumerationMethod() As Enumeration
Public Class EnumerationAdapter : Implements IEnumerable
Private Class EnumerationWrapper : Implements IEnumerator
Private m_Method As EnumerationMethod
Private m_Wrapped As Enumeration
Private m_Current As Object
Public Sub New(ByVal method As EnumerationMethod)
m_Method = method
End Sub
Public ReadOnly Property Current() As Object _
Implements IEnumerator.Current
Get
Return m_Current
End Get
End Property
Public Sub Reset() _
Implements IEnumerator.Reset
m_Wrapped = m_Method
If m_Wrapped Is Nothing Then
Throw New InvalidOperationException
End If
End Sub
Public Function MoveNext() As Boolean _
Implements IEnumerator.MoveNext
If m_Wrapped Is Nothing Then
Reset()
End If
Dim Result As Boolean = m_Wrapped.hasMoreElements()
If Result Then
m_Current = m_Wrapped.nextElement()
End If
Return Result
End Function
End Class
Private m_Method As EnumerationMethod
Public Function EnumerationAdapter(ByVal method As EnumerationMethod)
If method Is Nothing Then
Throw New ArgumentException
End If
m_Method = method
End Function
'IEnumerable
Public Function GetEnumerator() As IEnumerator _
Implements IEnumerable.GetEnumerator
Return New EnumerationWrapper(m_Method)
End Function
End Class
Public Delegate Function FilterEntryMethod(ByVal e As ZipEntry) As Boolean
Public Class ZipUtils
Public Shared Sub CopyStream(ByVal from As java.io.InputStream, _
ByVal tto As java.io.OutputStream)
Dim buffer() As System.SByte = New System.SByte(8192) {}
Dim got As Integer
While (got = from.read(buffer, 0, buffer.Length)) > 0
tto.write(buffer, 0, got)
End While
End Sub
Public Shared SubExtractZipFile(ByVal file As ZipFile, ByVal path As String, ByValfilter As FilterEntryMethod)
ForEach entry As ZipEntry In New EnumerationAdapter(NewEnumerationMethod(file.entries))
If Not entry.isDirectory() Then
If (filter = Nothing Or filter(entry)) Then
Dim s As java.io.InputStream = file.getInputStream(entry)
Try
Dim fname As String = System.IO.Path.GetFileName(entry.getName())
Dim Newpath As String = System.IO.Path.Combine(path,System.IO.Path.GetDirectoryName(entry.getName()))
System.IO.Directory.CreateDirectory(Newpath)
Dim dest As java.io.FileOutputStream = Newjava.io.FileOutputStream(System.IO.Path.Combine(Newpath, fname))
Try
CopyStream(s, dest)
Finally
dest.close()
End Try
Finally
s.close()
End Try
End If
End If
Next
End Sub
Public Shared Function CreateEmptyZipFile(ByVal fileName As String) As ZipFile
Dimzos As New ZipOutputStream(New java.io.FileOutputStream(fileName))
zos.close()
Return New ZipFile(fileName)
End Function
Public Shared FunctionUpdateZipFile(ByVal file As ZipFile, ByVal filter As FilterEntryMethod,_
ByVal newFiles As String())
Dim prev As String = file.getName
Dim tmp As String = System.IO.Path.GetTempFileName
Dimtto As ZipOutputStream = New ZipOutputStream(Newjava.io.FileOutputStream(tmp))
Try
CopyEntries(file, tto, filter)
If Not newFiles Is Nothing Then
For Each f As String In newFiles
Dim z As ZipEntry = New ZipEntry(f.Remove(0,System.IO.Path.GetPathRoot(f).Length))
z.setMethod(ZipEntry.DEFLATED)
tto.putNextEntry(z)
Try
Dim s As New java.io.FileInputStream(f)
Try
CopyStream(s, tto)
Finally
s.close()
End Try
Finally
tto.closeEntry()
End Try
Next
End If
Finally
tto.close()
End Try
file.close()
'now replace the old file with the new one
System.IO.File.Copy(tmp, prev, True)
System.IO.File.Delete(tmp)
Return New ZipFile(prev)
End Function
Public Shared Sub CopyEntries(ByVal from As ZipFile, ByVal tto As ZipOutputStream, _
ByVal filter As FilterEntryMethod)
ForEach entry As ZipEntry In New EnumerationAdapter(NewEnumerationMethod(from.entries))
If filter Is Nothing Or filter(entry) Then
Dim s As java.io.InputStream = from.getInputStream(entry)
Try
tto.putNextEntry(entry)
Try
CopyStream(s, tto)
Finally
tto.closeEntry()
End Try
Finally
s.close()
End Try
End If
Next
End Sub
End Class
End Namespace
try the convertor it is for both languageshttp://www.developerfusion.co.uk/utilities/convertvbtocsharp.aspx
I have tried a different translator before, and have just tried yoursuggestion. However, the results are the same and the conversion doesnot seem to be perfect. It still requires some manual tuning which Ihave done except for the two lines emphasized in red. Anyone here canhelp me out?
![Smile [:)]](http://pics.10026.com/?src=/emoticons/emotion-1.gif)
i know it is not perfect but what i do is deal with errors manually other wise you will not be albe to solve the problem... we can help you if you can not solve the error so you deal in that case with few lines of code rather than 100's lines
Well, the error I'm getting is for the two lines emphasizes in red:
c:\inetpub\wwwroot\TestApplication\Zip.vb(81): 'TestApplication.VbZip.EnumerationMethod' is a delegate type. Delegate construction permits only a single AddressOf expression as an argument list. Often an AddressOf expression can be used instead of a delegate construction.".
I don't know too well how to solve this, so any insight is appreciated.
if you specify the code that making the problem we might help you at the mean time look at this if that will help!!
http://abstractvb.com/code.asp?A=1084
http://www.startvbdotnet.com/language/enumeration.aspx
0 comments:
Post a Comment