BUZipArchive.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // BUZipArchive.h
  3. // BUZipArchive
  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 BUZipArchive : 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 delegate:(nullable id<BUZipArchiveDelegate>)delegate;
  32. + (BOOL)unzipFileAtPath:(NSString *)path
  33. toDestination:(NSString *)destination
  34. overwrite:(BOOL)overwrite
  35. password:(nullable NSString *)password
  36. error:(NSError * *)error;
  37. + (BOOL)unzipFileAtPath:(NSString *)path
  38. toDestination:(NSString *)destination
  39. overwrite:(BOOL)overwrite
  40. needRename:(BOOL)needRename
  41. password:(nullable NSString *)password
  42. error:(NSError * *)error
  43. delegate:(nullable id<BUZipArchiveDelegate>)delegate NS_REFINED_FOR_SWIFT;
  44. + (BOOL)unzipFileAtPath:(NSString *)path
  45. toDestination:(NSString *)destination
  46. preserveAttributes:(BOOL)preserveAttributes
  47. overwrite:(BOOL)overwrite
  48. password:(nullable NSString *)password
  49. error:(NSError * *)error
  50. delegate:(nullable id<BUZipArchiveDelegate>)delegate;
  51. + (BOOL)unzipFileAtPath:(NSString *)path
  52. toDestination:(NSString *)destination
  53. progressHandler:(void (^_Nullable)(NSString *entry, bu_unz_file_info zipInfo, long entryNumber, long total))progressHandler
  54. completionHandler:(void (^_Nullable)(NSString *path, BOOL succeeded, NSError * _Nullable error))completionHandler;
  55. + (BOOL)unzipFileAtPath:(NSString *)path
  56. toDestination:(NSString *)destination
  57. overwrite:(BOOL)overwrite
  58. password:(nullable NSString *)password
  59. progressHandler:(void (^_Nullable)(NSString *entry, bu_unz_file_info zipInfo, long entryNumber, long total))progressHandler
  60. completionHandler:(void (^_Nullable)(NSString *path, BOOL succeeded, NSError * _Nullable error))completionHandler;
  61. + (BOOL)unzipFileAtPath:(NSString *)path
  62. toDestination:(NSString *)destination
  63. preserveAttributes:(BOOL)preserveAttributes
  64. overwrite:(BOOL)overwrite
  65. needRename:(BOOL)needRename
  66. nestedZipLevel:(NSInteger)nestedZipLevel
  67. password:(nullable NSString *)password
  68. error:(NSError **)error
  69. delegate:(nullable id<BUZipArchiveDelegate>)delegate
  70. progressHandler:(void (^_Nullable)(NSString *entry, bu_unz_file_info zipInfo, long entryNumber, long total))progressHandler
  71. completionHandler:(void (^_Nullable)(NSString *path, BOOL succeeded, NSError * _Nullable error))completionHandler;
  72. // Zip
  73. // default compression level is Z_DEFAULT_COMPRESSION (from "zlib.h")
  74. // keepParentDirectory: if YES, then unzipping will give `directoryName/fileName`. If NO, then unzipping will just give `fileName`. Default is NO.
  75. // without password
  76. + (BOOL)createZipFileAtPath:(NSString *)path withFilesAtPaths:(NSArray<NSString *> *)paths;
  77. + (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath;
  78. + (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath keepParentDirectory:(BOOL)keepParentDirectory;
  79. // with optional password, default encryption is AES
  80. // don't use AES if you need compatibility with native macOS unzip and Archive Utility
  81. + (BOOL)createZipFileAtPath:(NSString *)path withFilesAtPaths:(NSArray<NSString *> *)paths withPassword:(nullable NSString *)password;
  82. + (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath withPassword:(nullable NSString *)password;
  83. + (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath keepParentDirectory:(BOOL)keepParentDirectory withPassword:(nullable NSString *)password;
  84. + (BOOL)createZipFileAtPath:(NSString *)path
  85. withContentsOfDirectory:(NSString *)directoryPath
  86. keepParentDirectory:(BOOL)keepParentDirectory
  87. withPassword:(nullable NSString *)password
  88. andProgressHandler:(void(^ _Nullable)(NSUInteger entryNumber, NSUInteger total))progressHandler;
  89. + (BOOL)createZipFileAtPath:(NSString *)path
  90. withContentsOfDirectory:(NSString *)directoryPath
  91. keepParentDirectory:(BOOL)keepParentDirectory
  92. compressionLevel:(int)compressionLevel
  93. password:(nullable NSString *)password
  94. AES:(BOOL)aes
  95. progressHandler:(void(^ _Nullable)(NSUInteger entryNumber, NSUInteger total))progressHandler;
  96. - (instancetype)init NS_UNAVAILABLE;
  97. - (instancetype)initWithPath:(NSString *)path NS_DESIGNATED_INITIALIZER;
  98. - (BOOL)open;
  99. /// write empty folder
  100. - (BOOL)writeFolderAtPath:(NSString *)path withFolderName:(NSString *)folderName withPassword:(nullable NSString *)password;
  101. /// write file
  102. - (BOOL)writeFile:(NSString *)path withPassword:(nullable NSString *)password;
  103. - (BOOL)writeFileAtPath:(NSString *)path withFileName:(nullable NSString *)fileName withPassword:(nullable NSString *)password;
  104. - (BOOL)writeFileAtPath:(NSString *)path withFileName:(nullable NSString *)fileName compressionLevel:(int)compressionLevel password:(nullable NSString *)password AES:(BOOL)aes;
  105. /// write data
  106. - (BOOL)writeData:(NSData *)data filename:(nullable NSString *)filename withPassword:(nullable NSString *)password;
  107. - (BOOL)writeData:(NSData *)data filename:(nullable NSString *)filename compressionLevel:(int)compressionLevel password:(nullable NSString *)password AES:(BOOL)aes;
  108. - (BOOL)close;
  109. @end
  110. @protocol BUZipArchiveDelegate <NSObject>
  111. @optional
  112. - (void)zipArchiveWillUnzipArchiveAtPath:(NSString *)path zipInfo:(bu_unz_global_info)zipInfo;
  113. - (void)zipArchiveDidUnzipArchiveAtPath:(NSString *)path zipInfo:(bu_unz_global_info)zipInfo unzippedPath:(NSString *)unzippedPath;
  114. - (BOOL)zipArchiveShouldUnzipFileAtIndex:(NSInteger)fileIndex totalFiles:(NSInteger)totalFiles archivePath:(NSString *)archivePath fileInfo:(bu_unz_file_info)fileInfo;
  115. - (void)zipArchiveWillUnzipFileAtIndex:(NSInteger)fileIndex totalFiles:(NSInteger)totalFiles archivePath:(NSString *)archivePath fileInfo:(bu_unz_file_info)fileInfo;
  116. - (void)zipArchiveDidUnzipFileAtIndex:(NSInteger)fileIndex totalFiles:(NSInteger)totalFiles archivePath:(NSString *)archivePath fileInfo:(bu_unz_file_info)fileInfo;
  117. - (void)zipArchiveDidUnzipFileAtIndex:(NSInteger)fileIndex totalFiles:(NSInteger)totalFiles archivePath:(NSString *)archivePath unzippedFilePath:(NSString *)unzippedFilePath;
  118. - (void)zipArchiveProgressEvent:(unsigned long long)loaded total:(unsigned long long)total;
  119. @end
  120. NS_ASSUME_NONNULL_END
  121. #endif /* _BUZipArchive_H */