Blitz Zip Userlib (Create an manipulate zip files)

BlitzPlus Forums/BlitzPlus Programming/Blitz Zip Userlib (Create an manipulate zip files)

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Wow .. this is an amazing library. I've managed to extract a .zip file today, and it works very nice.

I tried using one of my own archives. At first, the first example stopped after showing one file.
I changed to loop to:
Until ZipApi_GotoNextFile(zipIn) ;;ZIPAPI_UNZ_END_OF_LIST_OF_FILE
and it showed all files in the archive. Maybe that happened because my archive had only one file in the first folder, all other files are placed in subfolders.
Then I converted this example to extract the archive.
Because the .zip I used contained (sub)folders, I needed Mr. Picklesworth FixPath routine from the archive to create the folders. (For that routine, I needed to replace the "/" with "\" in the searchpath.)

Finally, this is the code I used:
Graphics 800, 600, 0, 2
; --------------------------------------------------
; --
; -- examples/example1.bb
; --
; -- A short example of how to iterate through a 
; -- ZIP file's contents using the ZipApi library.
; --
; --------------------------------------------------

; Include required libraries
Include "../Blitz_Basic_Bank.bb"
Include "../Blitz_File_FileName.bb"
Include "../Blitz_File_ZipApi.bb"

; Open the zip file
Local zipIn = ZipApi_Open("test-file.zip")

; Check the zip file was valid
If zipIn = 0 Then
	; Not a valid ZIP file - display error and exit program
	Print "There was an error whilst trying to open the zip file."
	WaitKey()
	End
EndIf

; Set file pointer to first file
ZipApi_GotoFirstFile(zipIn)

; Print a fancy header
Print LSet("FileName", 30) + RSet("Packed Size", 14) + RSet("Normal Size", 14) + RSet("Ratio", 10)

; Begin iterating through files
Repeat
	
	; Get the current file's information
	Local fileInfo.ZIPAPI_UnzFileInfo	= ZipApi_GetCurrentFileInfo(zipIn)
	
	; Generate fancy file data, containing file name, size and compression ratio
	Local fileData$	= ""
	Local compressionRatio# = Float((fileInfo\CompressedSize) / Float(fileInfo\UnCompressedSize)) * 100.0

	fn$ = Replace$(fileinfo\Filename, "/", "")
	FixPath(fn$)
	
	If FileType(fn$) <> 2 Then
		outname$ = ZipApi_ExtractFile(zipIn, fileinfo\Filename, fileinfo\Filename$)
	End If
		
	fileData$	= LSet(fileInfo\FileName, 30)
	fileData 	= fileData + RSet(fileInfo\CompressedSize, 14)
	fileData 	= fileData + RSet(fileInfo\UnCompressedSize, 14)
	fileData 	= fileData + RSet(compressionRatio + "%", 10)
	
	; Cleanup & output
	ZIPAPI_UnzFileInfo_Dispose(fileInfo)
	Print fileData
	
	
Until ZipApi_GotoNextFile(zipIn) ;;ZIPAPI_UNZ_END_OF_LIST_OF_FILE

; Close & Cleanup
ZipApi_Close(zipIn)

Print "Press any key.."
WaitKey()

End


;-----------------------------------------------------------------------------------------------------
;-----------------------------------------------------------------------------------------------------

; ID: 1567
; Author: Mr. Picklesworth
; Date: 2005-12-17 15:36:23
; Title: FixPath
; Description: Creates every missing folder in a file path

Function FixPath(path$)
;Creates every missing folder in a path. (Fills in gaps in a filepath)
;Handy for file saving.
;Written by Dylan McCall (Mr. Picklesworth)
	path$=extractfilepath(path$)
	Local c=1,pathTo$
	Repeat
		slash=Instr(path$,"",c)
		If slash=0
			If c>=Len(path$)+1
				Exit
			Else
				slash=Len(path$)+1
			EndIf
		EndIf
		
		folder$=Mid(path$,c,slash-c)
		If FileType(pathTo$+folder)=0 Then CreateDir(pathTo$+folder)
		c=slash+1
		pathTo$=pathTo$+folder+""
	Forever
	Return 1
End Function

;FUNCTION ExtractFilePath
;Accepts a filepath with filename and returns only the path
;i.e. pass c:\temp\test.txt the return value will be c:\temp\
Function ExtractFilePath$(sFilePath$) ;Written by Perturbatio
    ;LOCAL VARS
    Local iStartPos% = 0
    Local iSearchPos% = 0
    Local iFilePathLength = 0
    Local sFileExt$ = ""
    
    ;BEGIN FUNCTION CODE
    iFilePathLength = Len(sFilePath$)
    iSearchPos% = iFilePathLength
    
    While (iStartPos% < 1) And (iSearchPos% > 1)
        
        iStartPos% = Instr(sFilePath$, "", iSearchPos%)
        iSearchPos% = iSearchPos% - 1
        
    Wend
    
    If iStartPos = 0 Then ;if the filepath contains no backslashes
        sFileExt$ = sFilePath$
    Else
        sFileExt$ = Left$(sFilePath$, iStartPos%)
    EndIf
    
    
    Return sFileExt$    
End Function

Ow .. not really worth mentioning .. I needed to add WaitKey() commands to the examples to see their output.

The extract from/to banks functions seems very exciting, too. I can imagine this is very useful.
It works very well and thanks for showing this. I didn't knew this was possible at all.