Back  | Code Snippets |  [Bill's Home]

Zip and UnZip

[Java equivalent]

The Win32 API libraries contain a number of ZIP routines, such as:

· LZClose – Close a file handle which was opened with LZOpenFile.
· LZOpenFile = Open an LZ file stream.
· LZInit- Coverts a file handle from OpenFile() into a decompression format.
· LZCopy – Copes a source file to a destination. If the source is compressed, the destination will be decompressed.
· LZRead – Read from a ZIP file.
· LZSeek – Moves the file pointer of a file.

These are contained in the lz32.dll library. The following gives a VB program which compresses a file named 1.txt into 1.zip:

' Written by W.Buchanan, Aug 2003

Const OF_READ = &H0
Const OF_READWRITE = &H2
Const OF_REOPEN = &H8000
Const OF_CANCEL = &H800
Const OF_CREATE = &H1000
Const OF_DELETE = &H200
Const OF_EXIST = &H4000
Const OF_PARSE = &H100
Const OF_PROMPT = &H2000
Const OF_WRITE = &H1
Const OFS_MAXPATHNAME = 128

Private Type OFSTRUCT
cBytes As Byte
fFixedDisk As Byte
nErrCode As Integer
Reserved1 As Integer
Reserved2 As Integer
szPathName(OFS_MAXPATHNAME) As Byte
End Type

Private Declare Sub LZClose Lib "lz32.dll" (ByVal hfFile As Long)
Private Declare Function LZCopy Lib "lz32.dll" (ByVal hfSource As Long, ByVal hfDest As Long) As Long
Private Declare Sub LZDone Lib "lz32" ()
Private Declare Function LZInit Lib "lz32.dll" (ByVal hfSrc As Long) As Long
Private Declare Function LZRead Lib "lz32.dll" (ByVal hfFile As Long, ByVal lpvBuf As String, ByVal cbread As Long) As Long
Private Declare Function LZSeek Lib "lz32.dll" (ByVal hfFile As Long, ByVal lOffset As Long, ByVal nOrigin As Long) As Long
Private Declare Function LZStart Lib "lz32" () As Long
Private Declare Function LZOpenFile Lib "lz32.dll" Alias "LZOpenFileA" (ByVal lpszFile As String, lpOf As OFSTRUCT, ByVal style As Long) As Long

Private Sub Form_Load()

Dim lpOf As OFSTRUCT
Dim style As Long
Dim hsource As Long
Dim rtn As Long

hsource = LZOpenFile("c:\\java\\1.txt", lpOf, OF_READ)
hdest = LZOpenFile("c:\\java\\1.zip", lpOf, OF_WRITE)

rtn = LZCopy(hsource, hdest)
LZClose (hsource)
LZClose (hdest)
End Sub

The LZCopy routine returns value which identifies if the copy has been successful. These are:

Const LZERROR_BADINHANDLE = (-1) ' invalid input handle
Const LZERROR_BADOUTHANDLE = (-2) ' invalid output handle
Const LZERROR_READ = (-3) ' corrupt compressed file format
Const LZERROR_WRITE = (-4) ' out of space for output file
Const LZERROR_PUBLICLOC = (-5) ' insufficient memory for LZFile struct
Const LZERROR_GLOBLOCK = (-6) ' bad Global handle
Const LZERROR_BADVALUE = (-7) ' input parameter out of range
Const LZERROR_UNKNOWNALG = (-8) ' compression algorithm not recognized

For example:

rtn = LZCopy(hsource, hdest)
If (rtn = LZERROR_BADINHANDLE) Then
' msg for bad file handle
ElseIf (rtn = LZERROR_BADOUTHANDLE) Then
' msg for bad out handle
ElseIf (rtn = LZBADVALUE) Then
' msg for bad value
ElseIf (rtn = LZ_READ) Then
' msg for read
ElseIf (rtn = LZ_WRITE) Then
' msg for write
End If

The error values are negative, a positive value identifies the number of bytes copied.