zipfile.monkey2 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. Namespace std.zipfile
  2. Using miniz
  3. #rem monkeydoc @hidden
  4. #end
  5. Class ZipFile
  6. Property Files:String[]()
  7. Return _files
  8. End
  9. Method Close()
  10. If Not _data Return
  11. libc.free( _zip )
  12. _data.Discard()
  13. _data=Null
  14. End
  15. Method FindFile:Int( file:String )
  16. For Local i:=0 Until _files.Length
  17. If file=_files[i] Return i
  18. Next
  19. Return -1
  20. End
  21. Method Extract:Bool( dir:String,prefix:String="" )
  22. If Not dir.EndsWith( "/" ) dir+="/"
  23. Local result:=True
  24. For Local i:=0 Until _files.Length
  25. Local name:=_files[i]
  26. If Not name Continue
  27. If prefix And Not name.StartsWith( prefix ) Continue
  28. Local size:=_sizes[i]
  29. Local buf:=New DataBuffer( size )
  30. If mz_zip_reader_extract_to_mem( _zip,i,buf.Data,size,0 )
  31. Local path:=dir+name
  32. filesystem.CreateDir( ExtractDir( path ) )
  33. buf.Save( path )
  34. Else
  35. result=False
  36. Endif
  37. buf.Discard()
  38. Next
  39. Return result
  40. End
  41. Function Open:ZipFile( path:String )
  42. Local data:=DataBuffer.Load( path )
  43. If Not data Return Null
  44. Local zip:mz_zip_archive Ptr
  45. zip=Cast<mz_zip_archive Ptr>( libc.malloc( sizeof( zip[0] ) ) )
  46. memset( zip,0,sizeof( zip[0] ) )
  47. If Not mz_zip_reader_init_mem( zip,data.Data,data.Length,0 )
  48. free( zip )
  49. data.Discard()
  50. Return Null
  51. Endif
  52. Return New ZipFile( data,zip )
  53. End
  54. Private
  55. Field _data:DataBuffer
  56. Field _zip:mz_zip_archive Ptr
  57. Field _files:String[]
  58. Field _sizes:Int[]
  59. Method New( data:DataBuffer,zip:mz_zip_archive Ptr )
  60. _data=data
  61. _zip=zip
  62. Local nfiles:=mz_zip_reader_get_num_files( _zip )
  63. _files=New String[nfiles]
  64. _sizes=New Int[nfiles]
  65. For Local i:=0 Until nfiles
  66. Local stat:mz_zip_archive_file_stat
  67. If Not mz_zip_reader_file_stat( zip,i,Varptr stat ) Continue
  68. _files[i]=String.FromCString( stat.m_filename )
  69. _sizes[i]=stat.m_uncomp_size
  70. Next
  71. End
  72. End
  73. #rem monkeydoc Extracts a zip file to a directory.
  74. @param zipFile File path of zipfile to extract.
  75. @param dstDir Directory to extract zip file to.
  76. @return True if successful.
  77. #end
  78. Function ExtractZip:Bool( zipFile:String,dstDir:String )
  79. Return ExtractZip( zipFile,dstDir )
  80. End
  81. #rem monkeydoc @hidden
  82. #end
  83. Function ExtractZip:Bool( zipFile:String,dstDir:String,prefix:String )
  84. Local zip:=ZipFile.Open( zipFile )
  85. If Not zip Return False
  86. Local result:=zip.Extract( dstDir,prefix )
  87. zip.Close()
  88. Return result
  89. End