how to zip a file using vb6
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: how to zip a file using vb6

  1. #1
    Join Date
    Jul 2003
    Location
    Munich, Germany
    Posts
    51

    how to zip a file using vb6

    Hi,
    I want to zip a file.
    Is there an easy way to do this?

    I have downloaded the zlib dll (see http://www.gzip.org/zlib).

    And I have tried the following code:

    Declare Function compress Lib "zlib1.dll" _
    (ByVal dest As Long, ByVal destlen As Any, _
    ByVal source As Long, ByVal sourceLen As Long) As Integer

    Sub Main()
    Dim source() As Byte 'contents of the source file as byte array
    Dim dest() As Byte 'contents of the dest (zipped) file as byte array
    Dim destlen As Long 'number of bytes of the zipped file
    Dim r as long 'return value

    source = GetFileContents .... 'get the file contents as byte array
    ReDim destb(UBound(source)) 'dim the dest byte array at least of the size as the source
    r = compress(VarPtr(dest(0)), VarPtr(destlen), VarPtr(sourceb(0)), srclen) 'zip it
    Redim Preserve dest(destlen) 'destlen should now contain the length of the ziped file (dest())
    ... (write dest() to ta file
    End Sub


    It seems the zipping is working (I can see sth in dest() :)
    But the destlen is always 0 confused: , though I do not know the length of the dest to write to the file?
    any idea?

    thanks,
    Christoph

  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Did you read Chris Eastwood article?

    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    Nov 2003
    Location
    Seattle, WA
    Posts
    265
    Code:
    Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
    Public Declare Function compress2 Lib "zlib.dll" (Dest As Any, destLen As Any, Src As Any, ByVal srcLen As Long, ByVal Level As Long) As Long
    Public Declare Function uncompress Lib "zlib.dll" (Dest As Any, destLen As Any, Src As Any, ByVal srcLen As Long) As Long
    
    Dim FileData() As Byte
    Open "somefile.bin" For Binary Access Read As #1
        ReDim FileData(LOF(1))
        Get #1, , 1
    Close #1
    
    Open "someotherfile.bin" For Binary Access Write Lock Read Write As #1
        CompressBytes FileData(), 9
        Put #1, , FileData()
    Close #1
    
    Public Sub CompressBytes(Bytes() As Byte, Level As Integer)
        Dim BuffSize As Long
        Dim TBuff() As Byte
        
        BuffSize = UBound(Bytes) + 1
        BuffSize = BuffSize + (BuffSize * 1.01) + 12
        ReDim TBuff(BuffSize)
        
        compress2 TBuff(0), BuffSize, Bytes(0), UBound(Bytes) + 1, Level
        
        ReDim Bytes(BuffSize - 1)
        
        CopyMemory Bytes(0), TBuff(0), BuffSize
    End Sub
    
    Public Sub UnCompressBytes(Bytes() As Byte, OriginalSize As Long)
        Dim BuffSize As Long
        Dim TBuff() As Byte
        
        BuffSize = OriginalSize
        BuffSize = BuffSize + (BuffSize * 1.01) + 12
        ReDim TBuff(BuffSize)
        
        uncompress TBuff(0), BuffSize, Bytes(0), UBound(Bytes) + 1
        
        ReDim Bytes(BuffSize - 1)
        
        CopyMemory Bytes(0), TBuff(0), BuffSize
    End Sub
    I have used this to make my own binary file archiver.. works well too.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center