ZipArchive.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. // @header ZipArchive.h
  3. //
  4. // An objective C wrapper for minizip and libz for creating and exanding ZIP files.
  5. //
  6. // @author Created by aish on 08-9-11.
  7. // acsolu@gmail.com
  8. // @copyright Copyright 2008 Inc. All rights reserved.
  9. //
  10. */
  11. #import <Foundation/Foundation.h>
  12. typedef NS_ENUM(NSInteger, ZipArchiveCompression) {
  13. ZipArchiveCompressionDefault = -1,
  14. ZipArchiveCompressionNone = 0,
  15. ZipArchiveCompressionSpeed = 1,
  16. ZipArchiveCompressionBest = 9,
  17. };
  18. /**
  19. a block that is called from UnzipFileTo:overwrite:withProgressBlock: where the percentage of
  20. files processed (as an integer from 0 to 100), the number of files processed so far and the
  21. total number of files in the archive is called after each file is processed.
  22. */
  23. typedef void(^ZipArchiveProgressUpdateBlock)(int percentage, int filesProcessed, unsigned long numFiles);
  24. /**
  25. @protocol
  26. @discussion methods for a delegate to receive error notifications and control overwriting of files
  27. */
  28. @protocol ZipArchiveDelegate <NSObject>
  29. @optional
  30. /**
  31. @brief Delegate method to be notified of errors
  32. ZipArchive calls this selector on the delegate when errors are encountered.
  33. @param msg a string describing the error.
  34. @result void
  35. */
  36. -(void) ErrorMessage:(NSString*) msg;
  37. /**
  38. @brief Delegate method to determine if a file should be replaced
  39. When an zip file is being expanded and a file is about to be replaced, this selector
  40. is called on the delegate to notify that file is about to be replaced. The delegate method
  41. should return YES to overwrite the file, or NO to skip it.
  42. @param file - path to the file to be overwritten.
  43. @result a BOOL - YES to replace, NO to skip
  44. */
  45. -(BOOL) OverWriteOperation:(NSString*) file;
  46. @end
  47. /**
  48. @class
  49. @brief An object that can create zip files and expand existing ones.
  50. This class provides methods to create a zip file (optionally with a password) and
  51. add files to that zip archive.
  52. It also provides methods to expand an existing archive file (optionally with a password),
  53. and extract the files.
  54. */
  55. @interface ZipArchive : NSObject {
  56. @private
  57. void* _zipFile;
  58. void* _unzFile;
  59. unsigned long _numFiles;
  60. NSString* _password;
  61. id _delegate;
  62. ZipArchiveProgressUpdateBlock _progressBlock;
  63. NSArray* _unzippedFiles;
  64. NSFileManager* _fileManager;
  65. NSStringEncoding _stringEncoding;
  66. }
  67. /** a delegate object conforming to ZipArchiveDelegate protocol */
  68. @property (nonatomic, retain) id<ZipArchiveDelegate> delegate;
  69. @property (nonatomic, readonly) unsigned long numFiles;
  70. @property (nonatomic, copy) ZipArchiveProgressUpdateBlock progressBlock;
  71. @property (nonatomic, assign) ZipArchiveCompression compression;
  72. /**
  73. @brief String encoding to be used when interpreting file names in the zip file.
  74. */
  75. @property (nonatomic, assign) NSStringEncoding stringEncoding;
  76. /** an array of files that were successfully expanded. Available after calling UnzipFileTo:overWrite: */
  77. @property (nonatomic, readonly) NSArray* unzippedFiles;
  78. -(id) initWithFileManager:(NSFileManager*) fileManager;
  79. -(BOOL) CreateZipFile2:(NSString*) zipFile;
  80. -(BOOL) CreateZipFile2:(NSString*) zipFile append:(BOOL)isAppend;
  81. -(BOOL) CreateZipFile2:(NSString*) zipFile Password:(NSString*) password;
  82. -(BOOL) CreateZipFile2:(NSString*) zipFile Password:(NSString*) password append:(BOOL)isAppend;
  83. -(BOOL) addFileToZip:(NSString*) file newname:(NSString*) newname;
  84. -(BOOL) addDataToZip:(NSData*) data fileAttributes:(NSDictionary *)attr newname:(NSString*) newname;
  85. -(BOOL) CloseZipFile2;
  86. -(BOOL) UnzipOpenFile:(NSString*) zipFile;
  87. -(BOOL) UnzipOpenFile:(NSString*) zipFile Password:(NSString*) password;
  88. -(BOOL) UnzipFileTo:(NSString*) path overWrite:(BOOL) overwrite;
  89. -(NSDictionary *)UnzipFileToMemory;//To avoid memory issue, only use this method for small zip files.
  90. -(BOOL) UnzipCloseFile;
  91. // List the contents of the zip archive. must be called after UnzipOpenFile.
  92. // If zip file was appended with `CreateZipFile2:append:` or ``CreateZipFile2:Password:append:`,
  93. // `getZipFileContents` result won't be updated until re-unzip-open after close write handle (`CloseZipFile2` then `UnzipCloseFile` then (`UnzipOpenFile:` or `UnzipOpenFile:Password`) get called).
  94. -(NSArray*) getZipFileContents;
  95. @end