zipengine password checking?

BlitzMax Forums/BlitzMax Programming/zipengine password checking?

DougUK(Posted 2007) [#1]
Hi all,
I have figured out how to extract all folders and files from a zip etc.

but i need to be able to check the validity of the password when extracting a file to disk, does anyone know how to check if a password is true/false, so that the files are not extracted unless the correct password is used?


Chroma(Posted 2007) [#2]
This would be pretty useful.


tonyg(Posted 2007) [#3]
Isn't it
Method ExtractFile:TRamStream( fileName:String, caseSensitive:Int = False, password:String="" )
?
Unless of course you're not using the zipengine.mod


DougUK(Posted 2007) [#4]
I am using zipengine.mod :)

but extractfile actually extractsthe file whether the pass is right or wrong, if it is correct then it extracts properly, but if wrong it still extracts but encripted which i dont want.

So i need to know how to check the pass is correct or not, if you know what i mean :)


Damien Sturdy(Posted 2007) [#5]
I wouldn't have thought you can...

Extract a file and check wheather it came out OK, and if it didn't, say the password was wrong. :)

If there was a way to see if the password was correct other than this, the archives would be easily hackable.


DougUK(Posted 2007) [#6]
It looks like numberOfBytes is what controls the password, if it is >0 then the password is correct and seems to come up with -3 if incorrect.

Anyone fancy trying this out and see if they get the same?


Method ExtractFileToDisk( fileName:String, outputFileName:String, caseSensitive:Int = False, password:String="" )
		Local extractedFile:TRamStream = ExtractFile ( fileName, caseSensitive, password )
		Local entrySize:Int = unzGetCurrentFileSize( m_zipFile )
		Local stream:TRamStream=ZipRamStream.ZCreate(entrySize,True,False)		
		Local numberOfBytes:Int = unzReadCurrentFile ( m_zipFile, stream._buf, entrySize )		
		If numberOfBytes<0 Then
			DebugLog("Password is invalid")
						
		Else
			Local outFile:TStream = WriteFile ( outputFileName )
			DebugLog("Password is ok")
			If ( outFile And extractedFile ) Then			
				CopyStream( extractedFile, outFile )
			End If 
			CloseStream( outFile )
		EndIf
	End Method





Framework BRL.Basic
Import pub.zipengine

' Create our zipwriter object
Global zrObject:ZipReader = New ZipReader
Global password$="aPassword"
readZip("filename")


Function readZip(filename$)	
	' Open the zip file we just created and extract
	' a file
	

	If ( zrObject.OpenZip(CurrentDir()+"/"+filename$+".zip") ) Then		
		' unzip information		
		For Local i:Int=0 To zrObject.getFileCount()-1	
			Print("filename: "+zrObject.getFileInfo(i).zipFileName)
			Print("simplefilename: "+zrObject.getFileInfo(i).simpleFileName)
			Print("path: "+zrObject.getFileInfo(i).path)
			Print("orig size: "+zrObject.getFileInfo(i).header.DataDescriptor.uncompressedsize)
			Print("comp size: "+zrObject.getFileInfo(i).header.DataDescriptor.compressedsize)		
			
			If zrObject.getFileInfo(i).header.DataDescriptor.uncompressedsize=0
				'do nothing, just check for folders and files that have zero data etc...			
			Else
				Local p$=zrObject.getFileInfo(i).path
				Local dir=ReadDir(CurrentDir()+"/"+p$)
				If Not dir 					
					CreateDir( CurrentDir()+"/"+p$,False )
				EndIf
				zrObject.ExtractFileToDisk(zrObject.getFileInfo(i).simpleFileName, CurrentDir()+"/"+zrObject.getFileInfo(i).simpleFileName, False, password$)			
							
			EndIf
		Next
		zrObject.CloseZip()
	End If	
End Function