SSZipArchive.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // SSZipArchive.h
  3. // SSZipArchive
  4. //
  5. // Created by Sam Soffes on 7/21/10.
  6. // Copyright (c) Sam Soffes 2010-2015. All rights reserved.
  7. //
  8. #ifndef _SSZIPARCHIVE_H
  9. #define _SSZIPARCHIVE_H
  10. #import <Foundation/Foundation.h>
  11. #include "SSZipCommon.h"
  12. NS_ASSUME_NONNULL_BEGIN
  13. extern NSString *const SSZipArchiveErrorDomain;
  14. typedef NS_ENUM(NSInteger, SSZipArchiveErrorCode) {
  15. SSZipArchiveErrorCodeFailedOpenZipFile = -1,
  16. SSZipArchiveErrorCodeFailedOpenFileInZip = -2,
  17. SSZipArchiveErrorCodeFileInfoNotLoadable = -3,
  18. SSZipArchiveErrorCodeFileContentNotReadable = -4,
  19. SSZipArchiveErrorCodeFailedToWriteFile = -5,
  20. SSZipArchiveErrorCodeInvalidArguments = -6,
  21. };
  22. @protocol SSZipArchiveDelegate;
  23. @interface SSZipArchive : 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<SSZipArchiveDelegate>)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. password:(nullable NSString *)password
  41. error:(NSError * *)error
  42. delegate:(nullable id<SSZipArchiveDelegate>)delegate NS_REFINED_FOR_SWIFT;
  43. + (BOOL)unzipFileAtPath:(NSString *)path
  44. toDestination:(NSString *)destination
  45. preserveAttributes:(BOOL)preserveAttributes
  46. overwrite:(BOOL)overwrite
  47. password:(nullable NSString *)password
  48. error:(NSError * *)error
  49. delegate:(nullable id<SSZipArchiveDelegate>)delegate;
  50. + (BOOL)unzipFileAtPath:(NSString *)path
  51. toDestination:(NSString *)destination
  52. progressHandler:(void (^_Nullable)(NSString *entry, unz_file_info zipInfo, long entryNumber, long total))progressHandler
  53. completionHandler:(void (^_Nullable)(NSString *path, BOOL succeeded, NSError * _Nullable error))completionHandler;
  54. + (BOOL)unzipFileAtPath:(NSString *)path
  55. toDestination:(NSString *)destination
  56. overwrite:(BOOL)overwrite
  57. password:(nullable NSString *)password
  58. progressHandler:(void (^_Nullable)(NSString *entry, unz_file_info zipInfo, long entryNumber, long total))progressHandler
  59. completionHandler:(void (^_Nullable)(NSString *path, BOOL succeeded, NSError * _Nullable error))completionHandler;
  60. + (BOOL)unzipFileAtPath:(NSString *)path
  61. toDestination:(NSString *)destination
  62. preserveAttributes:(BOOL)preserveAttributes
  63. overwrite:(BOOL)overwrite
  64. nestedZipLevel:(NSInteger)nestedZipLevel
  65. password:(nullable NSString *)password
  66. error:(NSError **)error
  67. delegate:(nullable id<SSZipArchiveDelegate>)delegate
  68. progressHandler:(void (^_Nullable)(NSString *entry, unz_file_info zipInfo, long entryNumber, long total))progressHandler
  69. completionHandler:(void (^_Nullable)(NSString *path, BOOL succeeded, NSError * _Nullable error))completionHandler;
  70. // Zip
  71. // default compression level is Z_DEFAULT_COMPRESSION (from "zlib.h")
  72. // keepParentDirectory: if YES, then unzipping will give `directoryName/fileName`. If NO, then unzipping will just give `fileName`. Default is NO.
  73. // without password
  74. + (BOOL)createZipFileAtPath:(NSString *)path withFilesAtPaths:(NSArray<NSString *> *)paths;
  75. + (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath;
  76. + (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath keepParentDirectory:(BOOL)keepParentDirectory;
  77. // with optional password, default encryption is AES
  78. // don't use AES if you need compatibility with native macOS unzip and Archive Utility
  79. + (BOOL)createZipFileAtPath:(NSString *)path withFilesAtPaths:(NSArray<NSString *> *)paths withPassword:(nullable NSString *)password;
  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. - (instancetype)init NS_UNAVAILABLE;
  95. - (instancetype)initWithPath:(NSString *)path NS_DESIGNATED_INITIALIZER;
  96. - (BOOL)open;
  97. /// write empty folder
  98. - (BOOL)writeFolderAtPath:(NSString *)path withFolderName:(NSString *)folderName withPassword:(nullable NSString *)password;
  99. /// write file
  100. - (BOOL)writeFile:(NSString *)path withPassword:(nullable NSString *)password;
  101. - (BOOL)writeFileAtPath:(NSString *)path withFileName:(nullable NSString *)fileName withPassword:(nullable NSString *)password;
  102. - (BOOL)writeFileAtPath:(NSString *)path withFileName:(nullable NSString *)fileName compressionLevel:(int)compressionLevel password:(nullable NSString *)password AES:(BOOL)aes;
  103. /// write data
  104. - (BOOL)writeData:(NSData *)data filename:(nullable NSString *)filename withPassword:(nullable NSString *)password;
  105. - (BOOL)writeData:(NSData *)data filename:(nullable NSString *)filename compressionLevel:(int)compressionLevel password:(nullable NSString *)password AES:(BOOL)aes;
  106. - (BOOL)close;
  107. @end
  108. @protocol SSZipArchiveDelegate <NSObject>
  109. @optional
  110. - (void)zipArchiveWillUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo;
  111. - (void)zipArchiveDidUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo unzippedPath:(NSString *)unzippedPath;
  112. - (BOOL)zipArchiveShouldUnzipFileAtIndex:(NSInteger)fileIndex totalFiles:(NSInteger)totalFiles archivePath:(NSString *)archivePath fileInfo:(unz_file_info)fileInfo;
  113. - (void)zipArchiveWillUnzipFileAtIndex:(NSInteger)fileIndex totalFiles:(NSInteger)totalFiles archivePath:(NSString *)archivePath fileInfo:(unz_file_info)fileInfo;
  114. - (void)zipArchiveDidUnzipFileAtIndex:(NSInteger)fileIndex totalFiles:(NSInteger)totalFiles archivePath:(NSString *)archivePath fileInfo:(unz_file_info)fileInfo;
  115. - (void)zipArchiveDidUnzipFileAtIndex:(NSInteger)fileIndex totalFiles:(NSInteger)totalFiles archivePath:(NSString *)archivePath unzippedFilePath:(NSString *)unzippedFilePath;
  116. - (void)zipArchiveProgressEvent:(unsigned long long)loaded total:(unsigned long long)total;
  117. @end
  118. NS_ASSUME_NONNULL_END
  119. #endif /* _SSZIPARCHIVE_H */