This is some code I used to zip and unzip some files for a project I just finished. This code utilizes the j# langauge library. You might need to add a refrence to vsjlib. If you running a 64 bit server you may need to install the j# 64 bit redistibutable
vb
Imports java.util.zip
Imports java.io
Public Class Ziplib
Public Shared Function Uncompress(ByVal strZipFileName As String, ByVal strLocation As String) As Boolean
Try
Dim oFileInputStream As New java.io.FileInputStream(strZipFileName)
Dim oZipInputStream As New java.util.zip.ZipInputStream(oFileInputStream)
Dim bTrue As Boolean = True
Dim sbBuf(1024) As SByte
While 1 = 1
Dim oZipEntry As ZipEntry = oZipInputStream.getNextEntry()
If oZipEntry Is Nothing Then Exit While
If oZipEntry.isDirectory Then
If Not My.Computer.FileSystem.DirectoryExists(strLocation & oZipEntry.getName) Then
My.Computer.FileSystem.CreateDirectory(strLocation & oZipEntry.getName)
End If
Else
Dim oFileOutputStream As New java.io.FileOutputStream(strLocation.Replace("\", "/") & oZipEntry.getName()) While 1 = 1
Dim iLen As Integer = oZipInputStream.read(sbBuf)
If iLen < 0 Then Exit While
oFileOutputStream.write(sbBuf, 0, iLen)
End While
oFileOutputStream.close()
End If
End While
oZipInputStream.close()
oFileInputStream.close()
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
End Function
Public Shared Function Compress(ByVal strFileNames() As String, ByVal strZipFileName As String) As Boolean
Try
Compress = False
Dim oFileOutputStream As New java.io.FileOutputStream(strZipFileName)
Dim oZipOutputStream As New java.util.zip.ZipOutputStream(oFileOutputStream)
For Each strFileName As String In strFileNames
Dim oFileInputStream As New java.io.FileInputStream(strFileName)
Dim oZipEntry As New java.util.zip.ZipEntry(strFileName.Substring(3).Replace("\", "/")) oZipOutputStream.putNextEntry(oZipEntry)
Dim sbBuf(1024) As SByte
While 1 = 1
Dim iLen As Integer = oFileInputStream.read(sbBuf)
If iLen < 0 Then Exit While
oZipOutputStream.write(sbBuf, 0, iLen)
End While
oZipOutputStream.closeEntry()
oFileInputStream.close()
Next
oZipOutputStream.close()
oFileOutputStream.close()
Return True
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
End Function
End Class
c#
using java.util.zip;
using java.io;
public class Ziplib
{
public static bool Uncompress(string strZipFileName, string strLocation)
{ try { java.io.FileInputStream oFileInputStream = new java.io.FileInputStream(strZipFileName);
java.util.zip.ZipInputStream oZipInputStream = new java.util.zip.ZipInputStream(oFileInputStream);
bool bTrue = true;
sbyte[] sbBuf = new sbyte[1025];
while (1 == 1) { ZipEntry oZipEntry = oZipInputStream.getNextEntry();
if (oZipEntry == null)
break; // TODO: might not be correct. Was : Exit While
if (oZipEntry.isDirectory) { if (!My.Computer.FileSystem.DirectoryExists(strLocation + oZipEntry.getName)) { My.Computer.FileSystem.CreateDirectory(strLocation + oZipEntry.getName);
}
}
else { java.io.FileOutputStream oFileOutputStream = new java.io.FileOutputStream(strLocation.Replace("\\", "/") + oZipEntry.getName()); while (1 == 1) { int iLen = oZipInputStream.read(sbBuf);
if (iLen < 0)
break; // TODO: might not be correct. Was : Exit While
oFileOutputStream.write(sbBuf, 0, iLen);
}
oFileOutputStream.close();
}
}
oZipInputStream.close();
oFileInputStream.close();
}
catch (Exception ex) { throw new Exception(ex.Message);
}
}
public static bool Compress(string[] strFileNames, string strZipFileName)
{ bool functionReturnValue = false;
try { functionReturnValue = false;
java.io.FileOutputStream oFileOutputStream = new java.io.FileOutputStream(strZipFileName);
java.util.zip.ZipOutputStream oZipOutputStream = new java.util.zip.ZipOutputStream(oFileOutputStream);
foreach (string strFileName in strFileNames) { java.io.FileInputStream oFileInputStream = new java.io.FileInputStream(strFileName);
java.util.zip.ZipEntry oZipEntry = new java.util.zip.ZipEntry(strFileName.Substring(3).Replace("\\", "/")); oZipOutputStream.putNextEntry(oZipEntry);
sbyte[] sbBuf = new sbyte[1025];
while (1 == 1) { int iLen = oFileInputStream.read(sbBuf);
if (iLen < 0)
break; // TODO: might not be correct. Was : Exit While
oZipOutputStream.write(sbBuf, 0, iLen);
}
oZipOutputStream.closeEntry();
oFileInputStream.close();
}
oZipOutputStream.close();
oFileOutputStream.close();
return true;
}
catch (Exception ex) {
throw new Exception(ex.Message);
}
return functionReturnValue;
}
}
Technorati Tags:
c#,
vb#,
Code,
#ZIP