Decompressing the file or directory using VB.NET class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nagashree
    New Member
    • Mar 2008
    • 1

    Decompressing the file or directory using VB.NET class

    Hi,

    i want to compress the file or directory i am able to do that can anybody help me to decompress the file when i double click on the zipped file i am getting the error message as compressed(zipp ed) folder is invalid or corrupted. I am using VB.NET .Please help me out in this regard...Thank you in advance.

    Here is the code for that.

    Code:
    Public Class clsZip
    
    Public Sub CreateZipFile(ByVal sPath As String)
    Dim fos As java.io.FileOutputStream
    Dim zos As java.util.zip.ZipOutputStream
    Dim di As System.IO.DirectoryInfo
    
    'check , is it a file existing in this path, if true then zip a file
    If System.IO.File.Exists(sPath) Then
    Dim fInfo As New FileInfo(sPath)
    
    'create a zip file with same name in the same path
    fos = New java.io.FileOutputStream(sPath.Replace(fInfo.Exten sion, ".zip"))
    zos = New java.util.zip.ZipOutputStream(fos)
    
    'procedure to zip one File
    ZipOneFile(fos, zos, sPath)
    
    'check , is it a directory existing in this path,if true then zip a directory
    ElseIf System.IO.Directory.Exists(sPath) Then
    
    'create a zip file with same name in the same path
    fos = New java.io.FileOutputStream(sPath & ".zip")
    zos = New java.util.zip.ZipOutputStream(fos)
    di = New System.IO.DirectoryInfo(sPath)
    
    'procedure to zip a directory
    ZipDirectory(fos, zos, di, sPath)
    End If
    
    
    zos.close()
    fos.close()
    zos.flush()
    fos.flush()
    End Sub
    
    Private Sub ZipDirectory(ByVal fos As java.io.FileOutputStream, ByVal zos As java.util.zip.ZipOutputStream, ByVal di As System.IO.DirectoryInfo, ByVal SRootDir As String)
    Dim fis As java.io.FileInputStream
    Dim ze As java.util.zip.ZipEntry
    
    'to get file info from the directory
    Dim fInfos As System.IO.FileInfo() = di.GetFiles
    Dim fInfo As System.IO.FileInfo
    
    For Each fInfo In fInfos
    'give the zip entry or the folder arrangement for the file
    ze = New java.util.zip.ZipEntry(fInfo.FullName.Substring(SR ootDir.LastIndexOf("\")))
    
    'The DEFLATED method is the one of the methods to zip a file
    ze.setMethod(ze.DEFLATED)
    zos.putNextEntry(ze)
    
    'Input stream for the file to zip
    fis = New java.io.FileInputStream(fInfo.FullName)
    
    'Copy stream is a simple method to read a file input stream (file to zip) and write it to a file output stream(new zip file)
    CopyStream(fis, zos)
    
    zos.closeEntry()
    fis.close()
    Next
    
    'If the directory contains the sub directory the call the same procedure
    Dim dinfos As System.IO.DirectoryInfo() = di.GetDirectories()
    Dim dinfo As System.IO.DirectoryInfo
    For Each dinfo In dinfos
    ZipDirectory(fos, zos, dinfo, SRootDir)
    Next
    
    End Sub
    
    Private Sub ZipOneFile(ByVal fos As java.io.FileOutputStream, ByVal zos As java.util.zip.ZipOutputStream, ByVal sFullName As String)
    Dim fis As java.io.FileInputStream
    Dim ze As java.util.zip.ZipEntry
    'give the zip entry or the folder arrangement for the file
    ze = New java.util.zip.ZipEntry(sFullName.Substring(sFullNa me.LastIndexOf("\")))
    'The DEFLATED method is the one of the methods to zip a file
    ze.setMethod(ze.DEFLATED)
    zos.putNextEntry(ze)
    'Input stream for the file to zip
    fis = New java.io.FileInputStream(sFullName)
    'Copy stream is a simple method to read a file input stream (file to zip) and write it to a file output stream(new zip file)
    CopyStream(fis, zos)
    zos.closeEntry()
    fis.close()
    End Sub
    
    Private Sub CopyStream(ByVal src As java.io.FileInputStream, ByVal dest As java.util.zip.ZipOutputStream)
    Dim reader As New java.io.InputStreamReader(src)
    Dim writer As New java.io.OutputStreamWriter(dest)
    While reader.ready
    writer.write(reader.read)
    End While
    writer.flush()
    End Sub

    regards
    Nagashree
Working...