SSZipArchive.h 7.5 KB

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