SSZipArchive.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // SSZipArchive.h
  3. // SSZipArchive
  4. //
  5. // Created by Sam Soffes on 7/21/10.
  6. //
  7. #ifndef _SSZIPARCHIVE_H
  8. #define _SSZIPARCHIVE_H
  9. #import <Foundation/Foundation.h>
  10. #import "SSZipCommon.h"
  11. NS_ASSUME_NONNULL_BEGIN
  12. extern NSString *const SSZipArchiveErrorDomain;
  13. typedef NS_ENUM(NSInteger, SSZipArchiveErrorCode) {
  14. SSZipArchiveErrorCodeFailedOpenZipFile = -1,
  15. SSZipArchiveErrorCodeFailedOpenFileInZip = -2,
  16. SSZipArchiveErrorCodeFileInfoNotLoadable = -3,
  17. SSZipArchiveErrorCodeFileContentNotReadable = -4,
  18. SSZipArchiveErrorCodeFailedToWriteFile = -5,
  19. SSZipArchiveErrorCodeInvalidArguments = -6,
  20. };
  21. @protocol SSZipArchiveDelegate;
  22. @interface SSZipArchive : NSObject
  23. // Password check
  24. + (BOOL)isFilePasswordProtectedAtPath:(NSString *)path;
  25. + (BOOL)isPasswordValidForArchiveAtPath:(NSString *)path password:(NSString *)pw error:(NSError * _Nullable * _Nullable)error NS_SWIFT_NOTHROW;
  26. // Total payload size
  27. + (NSNumber *)payloadSizeForArchiveAtPath:(NSString *)path error:(NSError **)error;
  28. // Unzip
  29. + (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination;
  30. + (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination delegate:(nullable id<SSZipArchiveDelegate>)delegate;
  31. + (BOOL)unzipFileAtPath:(NSString *)path
  32. toDestination:(NSString *)destination
  33. overwrite:(BOOL)overwrite
  34. password:(nullable NSString *)password
  35. error:(NSError * *)error;
  36. + (BOOL)unzipFileAtPath:(NSString *)path
  37. toDestination:(NSString *)destination
  38. overwrite:(BOOL)overwrite
  39. password:(nullable NSString *)password
  40. error:(NSError * *)error
  41. delegate:(nullable id<SSZipArchiveDelegate>)delegate NS_REFINED_FOR_SWIFT;
  42. + (BOOL)unzipFileAtPath:(NSString *)path
  43. toDestination:(NSString *)destination
  44. preserveAttributes:(BOOL)preserveAttributes
  45. overwrite:(BOOL)overwrite
  46. password:(nullable NSString *)password
  47. error:(NSError * *)error
  48. delegate:(nullable id<SSZipArchiveDelegate>)delegate;
  49. + (BOOL)unzipFileAtPath:(NSString *)path
  50. toDestination:(NSString *)destination
  51. progressHandler:(void (^_Nullable)(NSString *entry, unz_file_info zipInfo, long entryNumber, long total))progressHandler
  52. completionHandler:(void (^_Nullable)(NSString *path, BOOL succeeded, NSError * _Nullable error))completionHandler;
  53. + (BOOL)unzipFileAtPath:(NSString *)path
  54. toDestination:(NSString *)destination
  55. overwrite:(BOOL)overwrite
  56. password:(nullable NSString *)password
  57. progressHandler:(void (^_Nullable)(NSString *entry, unz_file_info zipInfo, long entryNumber, long total))progressHandler
  58. completionHandler:(void (^_Nullable)(NSString *path, BOOL succeeded, NSError * _Nullable error))completionHandler;
  59. + (BOOL)unzipFileAtPath:(NSString *)path
  60. toDestination:(NSString *)destination
  61. preserveAttributes:(BOOL)preserveAttributes
  62. overwrite:(BOOL)overwrite
  63. nestedZipLevel:(NSInteger)nestedZipLevel
  64. password:(nullable NSString *)password
  65. error:(NSError **)error
  66. delegate:(nullable id<SSZipArchiveDelegate>)delegate
  67. progressHandler:(void (^_Nullable)(NSString *entry, unz_file_info zipInfo, long entryNumber, long total))progressHandler
  68. completionHandler:(void (^_Nullable)(NSString *path, BOOL succeeded, NSError * _Nullable error))completionHandler;
  69. // Zip
  70. // default compression level is Z_DEFAULT_COMPRESSION (from "zlib.h")
  71. // keepParentDirectory: if YES, then unzipping will give `directoryName/fileName`. If NO, then unzipping will just give `fileName`. Default is NO.
  72. // without password
  73. + (BOOL)createZipFileAtPath:(NSString *)path withFilesAtPaths:(NSArray<NSString *> *)paths;
  74. + (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath;
  75. + (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath keepParentDirectory:(BOOL)keepParentDirectory;
  76. // with optional password, default encryption is AES
  77. // don't use AES if you need compatibility with native macOS unzip and Archive Utility
  78. + (BOOL)createZipFileAtPath:(NSString *)path withFilesAtPaths:(NSArray<NSString *> *)paths withPassword:(nullable NSString *)password;
  79. + (BOOL)createZipFileAtPath:(NSString *)path withFilesAtPaths:(NSArray<NSString *> *)paths withPassword:(nullable NSString *)password progressHandler:(void(^ _Nullable)(NSUInteger entryNumber, NSUInteger total))progressHandler;
  80. + (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath withPassword:(nullable NSString *)password;
  81. + (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath keepParentDirectory:(BOOL)keepParentDirectory withPassword:(nullable NSString *)password;
  82. + (BOOL)createZipFileAtPath:(NSString *)path
  83. withContentsOfDirectory:(NSString *)directoryPath
  84. keepParentDirectory:(BOOL)keepParentDirectory
  85. withPassword:(nullable NSString *)password
  86. andProgressHandler:(void(^ _Nullable)(NSUInteger entryNumber, NSUInteger total))progressHandler;
  87. + (BOOL)createZipFileAtPath:(NSString *)path
  88. withContentsOfDirectory:(NSString *)directoryPath
  89. keepParentDirectory:(BOOL)keepParentDirectory
  90. compressionLevel:(int)compressionLevel
  91. password:(nullable NSString *)password
  92. AES:(BOOL)aes
  93. progressHandler:(void(^ _Nullable)(NSUInteger entryNumber, NSUInteger total))progressHandler;
  94. //suport symlink compress --file
  95. + (BOOL)createZipFileAtPath:(NSString *)path withFilesAtPaths:(NSArray<NSString *> *)paths withPassword:(nullable NSString *)password keepSymlinks:(BOOL)keeplinks;
  96. //suport symlink compress --directory
  97. + (BOOL)createZipFileAtPath:(NSString *)path
  98. withContentsOfDirectory:(NSString *)directoryPath
  99. keepParentDirectory:(BOOL)keepParentDirectory
  100. compressionLevel:(int)compressionLevel
  101. password:(nullable NSString *)password
  102. AES:(BOOL)aes
  103. progressHandler:(void(^ _Nullable)(NSUInteger entryNumber, NSUInteger total))progressHandler
  104. keepSymlinks:(BOOL)keeplinks;
  105. - (instancetype)init NS_UNAVAILABLE;
  106. - (instancetype)initWithPath:(NSString *)path NS_DESIGNATED_INITIALIZER;
  107. - (BOOL)open;
  108. - (BOOL)openForAppending;
  109. /// write empty folder
  110. - (BOOL)writeFolderAtPath:(NSString *)path withFolderName:(NSString *)folderName withPassword:(nullable NSString *)password;
  111. /// write file
  112. - (BOOL)writeFile:(NSString *)path withPassword:(nullable NSString *)password;
  113. - (BOOL)writeFileAtPath:(NSString *)path withFileName:(nullable NSString *)fileName withPassword:(nullable NSString *)password;
  114. - (BOOL)writeFileAtPath:(NSString *)path withFileName:(nullable NSString *)fileName compressionLevel:(int)compressionLevel password:(nullable NSString *)password AES:(BOOL)aes;
  115. ///write symlink files
  116. - (BOOL)writeSymlinkFileAtPath:(NSString *)path withFileName:(nullable NSString *)fileName compressionLevel:(int)compressionLevel password:(nullable NSString *)password AES:(BOOL)aes;
  117. /// write data
  118. - (BOOL)writeData:(NSData *)data filename:(nullable NSString *)filename withPassword:(nullable NSString *)password;
  119. - (BOOL)writeData:(NSData *)data filename:(nullable NSString *)filename compressionLevel:(int)compressionLevel password:(nullable NSString *)password AES:(BOOL)aes;
  120. - (BOOL)close;
  121. @end
  122. @protocol SSZipArchiveDelegate <NSObject>
  123. @optional
  124. - (void)zipArchiveWillUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo;
  125. - (void)zipArchiveDidUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo unzippedPath:(NSString *)unzippedPath;
  126. - (BOOL)zipArchiveShouldUnzipFileAtIndex:(NSInteger)fileIndex totalFiles:(NSInteger)totalFiles archivePath:(NSString *)archivePath fileInfo:(unz_file_info)fileInfo;
  127. - (void)zipArchiveWillUnzipFileAtIndex:(NSInteger)fileIndex totalFiles:(NSInteger)totalFiles archivePath:(NSString *)archivePath fileInfo:(unz_file_info)fileInfo;
  128. - (void)zipArchiveDidUnzipFileAtIndex:(NSInteger)fileIndex totalFiles:(NSInteger)totalFiles archivePath:(NSString *)archivePath fileInfo:(unz_file_info)fileInfo;
  129. - (void)zipArchiveDidUnzipFileAtIndex:(NSInteger)fileIndex totalFiles:(NSInteger)totalFiles archivePath:(NSString *)archivePath unzippedFilePath:(NSString *)unzippedFilePath;
  130. - (void)zipArchiveProgressEvent:(unsigned long long)loaded total:(unsigned long long)total;
  131. @end
  132. NS_ASSUME_NONNULL_END
  133. #endif /* _SSZIPARCHIVE_H */