Detailed explanation of the use of ssziprarchive

Keywords: github encoding

1, Use details:
When we develop an app, we sometimes need to compress and decompress the files. At this time, we must use a third-party open source library, ssziprarchive, to compress and decompress the target files.  

DDLog is used to record logs. The logs of DDLog exist in different files by day (set to save the logs for 7 days). The previous uploads will use NSData to read these 7 files in a circular way to generate a large NSData, which will be uploaded and decompressed into a file. However, it is very laborious for us to find the logs of a certain day, so we want to decompress the original 7 files, So you need to press the file under the log folder into a file, and then upload the file. After decompression, it is still a folder, and then you can find the log one day clearly. nice

github download address , it is recommended to use pod integration. At first, I integrated it manually, but after compilation, the compressed file is always damaged, and then I will replace it with pod integration

//compress
- (void)createZipFile {
    //Destination path
    NSString *destinationPath = @"/Users/Administrator/Desktop/wzg.zip";//Note that this is the suffix of zip format
    //Source file path
    NSString *sourceFilePath = @"/Users/Administrator/Desktop/wzg.txt";
    //Multiple source files can be placed in the array. These files will be packed into a compressed package in the same path as destinationPath.
    if ([SSZipArchive createZipFileAtPath:destinationPath withFilesAtPaths:@[sourceFilePath]]) {
        NSLog(@"Compression success");
    }
    else {
        NSLog(@"Compression failure");
    }
}
//decompression
- (void)unzipFile {
    //Source file path
    NSString *sourceFilePath = @"/Users/Administrator/Desktop/wzg.zip";
    //Destination file path
    NSString *destinationPath = @"/Users/wangzhengang/Desktop/";
    //Extract the zip package under the path of sourceFilePath to the path of destinationPath
    if ([SSZipArchive unzipFileAtPath:sourceFilePath toDestination:destinationPath delegate:self uniqueId:nil]){
        NSLog(@"Decompression success");
    }
    else {
        NSLog(@"Decompression failed");
    }
}


Proxy method:

#pragma mark - SSZipArchiveDelegate
- (void)zipArchiveWillUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo {
    NSLog(@"Will unzip.");
}
 
- (void)zipArchiveDidUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo unzippedPath:(NSString *)unzippedPat uniqueId:(NSString *)uniqueId {
    NSLog(@"Decompression complete!");
}

2, Ssziprarchive all method descriptions:

@interface SSZipArchive : NSObject
 
// Unzip decompression
/**
 * @param          path    source file
 * @param   destination    Destination file
 * @param      uniqueId    Tag to distinguish multiple decompression operations
 *
 * @return YES indicates success, NO indicates failure.
 */
+ (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination  uniqueId:(NSString *)uniqueId;
 
/**
 * @param          path    source file
 * @param   destination    Destination file
 * @param     overwrite    YES The file with the same name under the destination path will be overwritten, and NO will not.
 * @param      password    A compressed package that requires a password to be decompressed
 * @param         error    Returns the error information encountered during decompression
 * @param      uniqueId    Tag to distinguish multiple decompression operations
 *
 * @return YES indicates success, NO indicates failure.
 */
+ (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination overwrite:(BOOL)overwrite password:(NSString *)password error:(NSError **)error  uniqueId:(NSString *)uniqueId;
 
/**
 * @param          path    source file
 * @param   destination    Destination file
 * @param      delegate    Setting agent
 * @param      uniqueId    Tag to distinguish multiple decompression operations
 *
 * @return YES indicates success, NO indicates failure.
 */
+ (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination delegate:(id<SSZipArchiveDelegate>)delegate  uniqueId:(NSString *)uniqueId;
 
/**
 * @param          path    source file
 * @param   destination    Destination file
 * @param     overwrite    YES The file with the same name under the destination path will be overwritten, and NO will not.
 * @param      password    A compressed package that requires a password to be decompressed
 * @param         error    Returns the error information encountered during decompression
 * @param      delegate    Setting agent
 * @param      uniqueId    Tag to distinguish multiple decompression operations
 *
 * @return YES indicates success, NO indicates failure.
 */
+ (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination overwrite:(BOOL)overwrite password:(NSString *)password error:(NSError **)error delegate:(id<SSZipArchiveDelegate>)delegate uniqueId:(NSString *)uniqueId;
 
// Zip compression
/**
 * @param       path    Destination path (Format: ~ / path at the end of xxx.zip)
 * @param  filenames    File path to compress
 *
 * @return Returns YES for success and NO for compression failure.
 */
+ (BOOL)createZipFileAtPath:(NSString *)path withFilesAtPaths:(NSArray *)filenames;
 
/**
 * @param       path    Destination path (Format: ~ / path at the end of xxx.zip)
 * @param  filenames    File directory path to compress
 *
 * @return Returns YES for success and NO for compression failure.
 */
+ (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath;
 
/**
 * Initialize compression object
 *
 * @param  path    Destination path (Format: ~ / path at the end of xxx.zip)
 *
 * @return Image alignment after initialization
 */
- (id)initWithPath:(NSString *)path;
 
/**
 *  Open compressed object
 * @return Returns YES for success and NO for failure.
 */
- (BOOL)open;
 
/**
 * Add the path of the file to be compressed
 *
 * @param  path    File path
 *
 * @return Returns YES for success and NO for failure.
 */
- (BOOL)writeFile:(NSString *)path;
 
/**
 * Write data to the file in this path
 *
 * @param      data    Data to write
 * @param  filename    File path
 *
 * @return Returns YES for success and NO for failure.
 */
- (BOOL)writeData:(NSData *)data filename:(NSString *)filename;
 
/**
 *  Turn off compressed objects
 * @return Returns YES for success and NO for failure.
 */
- (BOOL)close;
 
@end
 
 
@protocol SSZipArchiveDelegate <NSObject>
 
@optional
 
//Will be decompressed
- (void)zipArchiveWillUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo;
//Decompression complete
- (void)zipArchiveDidUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo unzippedPath:(NSString *)unzippedPat uniqueId:(NSString *)uniqueId;
//Will be decompressed
- (void)zipArchiveWillUnzipFileAtIndex:(NSInteger)fileIndex totalFiles:(NSInteger)totalFiles archivePath:(NSString *)archivePath fileInfo:(unz_file_info)fileInfo;
//Decompression complete
- (void)zipArchiveDidUnzipFileAtIndex:(NSInteger)fileIndex totalFiles:(NSInteger)totalFiles archivePath:(NSString *)archivePath fileInfo:(unz_file_info)fileInfo;
 
@end

3, Problems encountered:
When the file name of the file to be compressed or decompressed contains Chinese text, there will be the problem of file name scrambling, or the problem that the decompressed file cannot be found under the destination path.
terms of settlement:
In the ssziprarchive. M file, change the encoding format of the file path.
Before change:

After change:

93 original articles published, 31 praised, 220000 visitors+
Private letter follow

Posted by jeff8j on Thu, 27 Feb 2020 00:00:16 -0800