Geoffrey Emery
Tech Goodness

Zip And Unizp Files in VB.net and C#

April 15, 2008 18:00 by gemery

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: ,,,

Tags: , ,
Categories: Code
Actions: E-mail | Permalink | Comments (11) | Comment RSSRSS comment feed

Related posts

Comments

April 18. 2008 06:50

Josh

The other option is
icsharpcode.net/.../Default.aspx

The sample provided as a download can be tweaked pretty easily to do whatever you want it to.

Josh

October 20. 2009 02:32

passing all drug test

Wonder full writing skills you got mate.


Regards
Mathu

passing all drug test

October 24. 2009 01:59

play superball keno

Useful information like this one must be kept and maintained so I will put this one on my bookmark list! Thanks for this wonderful post and hoping to post more of this!

play superball keno

October 28. 2009 22:35

SEO Reseller  

A new website crawled by spiders will be reported and recorded in a search engine's database according to the category or keyword that it has been perceived to belong to.

SEO Reseller  

October 29. 2009 07:06

Raleigh Painting

where do i get more information on this



Regards and respect
Like

Raleigh Painting

October 29. 2009 07:12

Language Training Resources

Wonder full writing skills you got mate.


Regards
Crosby


Language Training Resources

October 30. 2009 05:56

Computer Career Raleigh

Hi nice site design


Regards

Raines

Computer Career Raleigh

November 1. 2009 00:07

online personal loans

Interesting ... as always - is your blog making any cash advance ? ;)

online personal loans

November 5. 2009 05:38

faxless payday loans

I guess there's always an easier way ...

faxless payday loans

November 10. 2009 03:03

cash loans

Like your writing! Still you can do some things to improve it.

cash loans

November 10. 2009 04:24

wholesale bling

Admiring the time and effort you put into your blog and detailed information you offer! I will bookmark your blog and have my children check up here often. Thumbs up!

wholesale bling

Comments are closed