Hi Everyone,
I managed to get the zip/unzip functionality into the existing zlib module done today. Though it is a little rough and needs to be cleaned before I pass it on to the community. The code below takes a file, creates a zip file, adds the file, then gets it back again. Don't worry I'll make it much simpler to use before I submit it (with a much nicer interface).
Rem
Zip File demo 1 - Creating and adding to a zip file and getting it back!
Notes:
This example demonstrates the ability create a zip file and add files to
it.
End Rem
Import Pub.ZLib
Main() ' Call our entry point
Function Main()
Local inFile:TStream = OpenFile( "test.txt" )
Local zipFile:Byte Ptr = Null
If (inFile) Then
Local inSize:Int = StreamSize(inFile)
Local inData:Byte[] = New Byte[inSize]
For pos = 0 To inSize - 1
SeekStream( inFile, pos )
inData[pos] = ReadByte ( inFile )
Next
' Open the file in create mode
zipFile = zipOpen( "data.zip", APPEND_STATUS_CREATE )
' Open the test.txt as a new entry inside the zip
zipOpenNewFileInZip( zipFile, "test.txt", Null, Null, Null, Null, Null, Null, ..
Z_DEFLATED, Z_DEFAULT_COMPRESSION )
' Write the file into the zip
zipWriteInFileInZip( zipFile, inData, inSize )
zipClose( zipFile, "Our cool zip file" )
End If
' Now let's open it and get our file back out
Local result:Int = 0
Local outStream:TStream = WriteStream("new.txt")
Local numberOfBytes:Int
Local data:Byte Ptr
zipFile = unzOpen( "data.zip" )
If (zipFile) Then
result = unzLocateFile( zipFile, "test.txt", UNZ_NO_CASE_CHECK )
If ( result = UNZ_OK ) Then
result = unzOpenCurrentFile( zipFile )
If ( result = UNZ_OK ) Then
Local size = unzGetCurrentFileSize(zipFile)
data = New Byte Ptr[size]
numberOfBytes = unzReadCurrentFile (zipFile, data, size )
For i:Int = 1 To size
WriteByte( outStream, data[i] )
Next
CloseStream ( outStream )
End If
End If
unzClose( zipFile )
End if
End Function
|