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