Browse Source

WIP std.zipfile support.

Mark Sibly 8 years ago
parent
commit
096eedafe9

BIN
bananas/zipfiletest/blah.zip


+ 22 - 0
bananas/zipfiletest/zipfiletest.monkey2

@@ -0,0 +1,22 @@
+
+#Import "<std>"
+
+#Import "blah.zip@/"
+
+Using std..
+
+Function Main()
+
+	Local zip:=ZipFile.Open( "asset::blah.zip" )
+	
+	For Local file:=Eachin zip.Files
+		Print file
+	Next
+	
+	Local str:=zip.ExtractData( "geom/affinemat3.monkey2" ).PeekString( 0 )
+	
+	Print str
+	
+	zip.Close()
+
+End

+ 36 - 1
modules/std/misc/zipfile.monkey2

@@ -3,21 +3,54 @@ Namespace std.zipfile
 
 Using miniz
 
-#rem monkeydoc @hidden
+#rem monkeydoc The ZipFile class.
 #end
 Class ZipFile
 
+	#rem monkeydoc Array of all files contained in the zip.
+	#end
 	Property Files:String[]()
 		Return _files
 	End
 	
+	#rem monkeydoc Closes the zip.
+	#end
 	Method Close()
 		If Not _data Return
 		libc.free( _zip )
+		_files=Null
+		_sizes=Null
 		_data.Discard()
 		_data=Null
 	End
 	
+	#rem monkeydoc Checks if a file is contained in the zip.
+	#end
+	Method Contains:Bool( file:String )
+	
+		Return FindFile( file )<>-1
+	End
+	
+	#rem moneydoc Extracts a file from the zip into a databuffer.
+	#end
+	Method ExtractData:DataBuffer( file:String )
+	
+		Local i:=FindFile( file )
+		If i=-1 Return Null
+
+		Local size:=_sizes[i]
+
+		Local buf:=New DataBuffer( size )
+		
+		If mz_zip_reader_extract_to_mem( _zip,i,buf.Data,size,0 ) Return buf
+		
+		buf.Discard()
+		
+		Return Null
+	End
+
+	#rem monkeydoc @hidden
+	#end	
 	Method FindFile:Int( file:String )
 
 		For Local i:=0 Until _files.Length
@@ -27,6 +60,8 @@ Class ZipFile
 		Return -1
 	End
 	
+	#rem monkeydoc @hidden
+	#end	
 	Method Extract:Bool( dir:String,prefix:String="" )
 	
 		If Not dir.EndsWith( "/" ) dir+="/"