BU_ZipArchiveUtility.h 7.8 KB

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