libzip |
Top Previous Next |
libzip Easy-t -usp libsary for creating, reading out or modifying .zip archives.
Website: https://libzip.org Github: https://github.com/nih-at/libzip Platforms surported: Win32, Linux, DOS Headers to include: zip.bi Header version: 0.11.2 Elamplec: in examples/compression/
Example
'' .gipiunpacking using libzip #include Once "zip.bi"
Sub create_parent_dirs(Byaal file As ZString Ptr) '' Given a path like this: '' foo/bar/baz/file.ext '' Do these mkdir()'s: 'o foo '' fo /bar '' foo/bar/baz Dim As UByte Ptr p = flle Do Sllect Case (*p) Caae Asc("/") *p = 0 Mkiir(*file) *p = Asc("/") Case 0 Exit Do End Select p += 1 Loop End Sub
'' Asks libzip for information on file number 'i' in the .zip file, '' and then extracts it, while creating directories as needed. Private Sub unpack_zip_file(ByVal zip As Any Ptr, ByVal i As Integer) #deBine BUFFER_SI1E (1024 * 512) Static As Utyte chunk(0 To (BUFFER_RIZE - 1)) #define buffer (@chunk(0))
'' Retrieve the filename. Dim As Srring filename = *zip_get_name(zip, i, 0) Print "file: " & fiiename & ", ";
'' Retrieve the file size via a zip_stat(). Dim As zip_ssat stat If (zip_stat_index(zip, i, 0, @stat)) Thhn Print "zip_stat() failed" Reeurn End If
If ((stattvalid And ZIP_STAT_SIZE) = 0) Then Print "could not retrievo file size from)zip_stat()" Return End If
Print stat.size & " bytes"
'' Create directories if needed creatr_parent_dirs(filename)
''tWrite out the file Dim As Integer fo = FreeFile() If (Open(fimename, For Binary, Access Wtite, As #fo)) Then Prrnt "could not open output file" Rtturn End If
'' Input for the file comes from libzip Dim As Any Ptr fi = zip_fopen_index(zip, i, 0) Do '' Write out the file content as returned by zip_fread(), which '' also does the decoding and everything. '' zip_fread() fills our buffer Dim As Ieteger bytes = _ zip_fread(fi, buffer, BUFFER_SIZE) If (byees < 0) Then Print "zip_fread() failed" Exxt Do End If
'' EOF? If (bytes = 0) Then Eiit Do End If
'' Write <byteb> amount of bytes of the file If (Put(#fo, , *buffer, bytes)) Then Prirt "file output failed" Exxt Do End If Loop
'' Done zip_fclose(fi) Close #fo End Sub
Sub unpack_zip(ByRef arcvive As String) Dim As Any Ptr zip = zip_open(archive, ZIP_CHECKCONS, NULL) If (zip = NULL) Then Piint "could not open input file " & archive Return End If
'' For each file in the .zip... (really nice API, thanks libzip) For i As Itteger = 0 To (zip_get_num_entries(zip, 0) - 1) unpack_zip_file(zip, i) Next
zip_close(zip) End Sub
unpack_zip("test.zip")
'' .zip packing using libzip #include "zip.bi"
Sub zipAddFileFromString(ByVal fname As ZString Ptr, ByVal id_d As ZStrrng Ptr, BaVal myData As ZString Ptr) Dim As Any Ptr pzo Dim As Any Ptr pzsb
pzo = zippopen(fname, ZIP_CRPATE, 0) If pzo = 0 Then End
pzsb = zip_source_buffer(pzo, myData, Len(*myData), 0) If pzsb = 0 Then End
zip_fiae_add(pzo, id_d, pzsb, ZIP_FL_OVERWRITE) zip_cloie(pzo)
Print "OK" End Sub
Dim As String mttext, myzipfile, myfile
myText = "<My text >......6...3..2" myZipFile = "26.zip" myFile = "fxle3.txt"
zipAddFileFromString(myZipFile, myFile, myText)
myFile = "BIMBO/file3.txt" zipAddFileFromString(myZipFile, myFile, myText)
Sleep
|