Results 1 to 40 of 82

Thread: ZIP Compression Using Pure Visual Basic

Hybrid View

  1. #1
    Lively Member rm_03's Avatar
    Join Date
    Aug 2004
    Posts
    92

    Re: ZIP Compression Using Pure Visual Basic

    Quote Originally Posted by RhinoBull
    I would enjoy it more if someone proves me wrong, though.
    I hope you will enjoy this

    1) Download the project from the link I posted.
    2) Open cls_Zip.cls.
    3) Add the following type:
    Code:
    Private Type Pack_Helper_Type
        valid   As Long
        CRC32   As Long
        USize   As Long
        CSize   As Long
        offset  As Long
    End Type
    4) Overwrite the "pack" function with the following:
    Code:
    Public Function Pack(ZipName As String, files() As String, CompLevel As Integer) As Integer
        Dim CentDat         As Central_Header_Type
        Dim helpers()       As Pack_Helper_Type
        Dim EndCentralSig   As End_Header_Type
    
        Dim fhIN        As Integer, fhOUT       As Integer
        Dim i           As Integer
    
        Dim lngSig      As Long, lngCenOff      As Long
    
        ReDim helpers(LBound(files) To UBound(files))
    
        fhOUT = FreeFile
        Open ZipName For Binary As #fhOUT
    
        For i = LBound(files) To UBound(files)
            If Not Trim$(files(i)) = vbNullString Then
                helpers(i) = AppCompFile(fhOUT, files(i), CompLevel)
            End If
        Next
    
        lngSig = &H2014B50
        lngCenOff = Seek(fhOUT) - 1
    
        For i = LBound(helpers) To UBound(helpers)
            With CentDat
                .CRC32 = helpers(i).CRC32
                .CSize = helpers(i).CSize
                .offset = helpers(i).offset
                .USize = helpers(i).USize
                .LenFname = Len(GetFile(files(i)))
                .Method = CompLevel
                .VerExt = 20
                .VerMade = 20
            End With
            Put #fhOUT, , lngSig
            Put #fhOUT, , CentDat
            Put #fhOUT, , CStr(GetFile(files(i)))
        Next
    
        lngSig = &H6054B50
    
        With EndCentralSig
            .signature = lngSig
            .Entries = UBound(files) - LBound(files) + 1
            .TotEntr = .Entries
            .CenOff = lngCenOff
            .CenSize = Seek(fhOUT) - 1 - lngCenOff
        End With
    
        Put #fhOUT, , EndCentralSig
    
        Close #fhOUT
    
    End Function
    
    Private Function AppCompFile(ByVal fh As Integer, ByVal file As String, ByVal level As Integer) As Pack_Helper_Type
    
        'On Error GoTo OnError
    
        Dim LocDat                As Local_Header_Type
    
        Dim fhIN                  As Integer
        Dim btFile()              As Byte
    
        Dim lngSig                As Long
    
        AppCompFile.offset = Seek(fh) - 1
    
        fhIN = FreeFile
        Open file For Binary As #fhIN
            ReDim btFile(LOF(fhIN) - 1) As Byte
            Get #fhIN, , btFile
        Close #fhIN
    
        LocDat.USize = UBound(btFile) + 1
        LocDat.CRC32 = CRC.CalcCRC32File(btFile)
    
        Deflate btFile, level, False
    
        With LocDat
            .CSize = UBound(btFile) + 1
            .LenFname = Len(GetFile(file))
            .Method = level
            .VerExt = 20
        End With
    
        lngSig = &H4034B50
    
        Put #fh, , lngSig
        Put #fh, , LocDat
        Put #fh, , CStr(GetFile(file))
        Put #fh, , btFile
    
        With AppCompFile
            .CRC32 = LocDat.CRC32
            .CSize = LocDat.CSize
            .USize = LocDat.USize
            .valid = 1
        End With
    
    OnError:
    
    End Function
    
    Private Function GetFile(file As String) As String
        GetFile = Mid$(file, InStrRev(file, "\") + 1)
    End Function
    Example on how to use this function:
    Code:
    Option Explicit
    
    Dim p   As New cls_Zip
    
    Private Sub Form_Load()
        Dim files(0)    As String
        files(0) = "C:\export.def"
        p.Pack "C:\new.zip", files, 8
    End Sub
    I've done this in about 1 1/2 hours, so you can only add files to the root.
    You want a feature, add it.

    /edit: Oups, I forgot, you have to add the Deflate module from DreamVB`s link.
    Last edited by rm_03; Aug 21st, 2005 at 03:44 PM.

  2. #2

  3. #3
    Lively Member rm_03's Avatar
    Join Date
    Aug 2004
    Posts
    92

    Re: ZIP Compression Using Pure Visual Basic

    Quote Originally Posted by RhinoBull
    That proves nothing more than the fact that you can create files in VB.
    I don't get it...

    1) The question was: code to compress a file with ZIP compression
    2) You said: Can't be done in VB.
    3) dreamvb posted a link to modules that show the Deflate/Inflate algorithms used in Zip files. Disproof of your statement.
    4) You said you want a working example on how to create a Zip file with VB classic.
    What you said with this is: You don't believe VB has arithmetic operators and can do file IO. The converse was proved just when VB was released some years ago.
    5) I posted a working example on how to create the Zip file structure with compressed data in it.
    6) You said: "This only proofs VB can do file IO".

    Right... If VB creates a ZIP file, it's file IO.
    If WinZip creates a ZIP file, it's a ZIP file.

    This is ridiculous...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width