ssziparchive怎么使用与ziparchive哪个好用

germanocaldeira-baker
Baker.xcodeproj
project.xcworkspace
xcuserdata
ericwang.xcuserdatad
UserInterfaceState.xcuserstate
contents.xcworkspacedata
xcuserdata
ericwang.xcuserdatad
Baker.xcscheme
UnitTests.xcscheme
xcschememanagement.plist
project.pbxproj
background-landscape.png
background-portrait.png
background.jpg
baker-badge.png
baker-framework-badge.png
blood-drop.png
cover-image.png
dots-menu.png
1-01-a.gif
1-01-b.gif
1-02-a.gif
1-02-b.gif
1-03-a.gif
1-03-b.gif
1-04-a.gif
1-04-b.gif
1-05-a.gif
1-06-a.gif
1-06-b.gif
1-07-a.gif
1-07-b.gif
2-01-a.gif
2-01-b.gif
2-02-a.gif
2-02-b.gif
2-03-a.gif
2-04-a.gif
2-04-b.gif
2-05-a.gif
2-06-a.gif
2-06-b.gif
2-07-a.gif
Book Cover.html
Book Index.html
index.html
Part1-01.html
Part1-02.html
Part1-03.html
Part1-04.html
Part1-05.html
Part1-06.html
Part1-07.html
Part2-01.html
Part2-02.html
Part2-03.html
Part2-04.html
Part2-05.html
Part2-06.html
Part2-07.html
ios-icon-ipad.png
ios-icon-ipad@2x.png
ios-icon-iphone.png
ios-icon-iphone@2x.png
OCMock.framework
Info.plist
License.txt
.gitignore
back@2x.png
Baker-Info.plist
Baker_Prefix.pch
forward.png
forward@2x.png
libOCMock.a
UnitTests-Info.plist
germanocaldeira-baker
Baker.xcodeproj
project.xcworkspace
._contents.xcworkspacedata
._project.pbxproj
._project.xcworkspace
._book.css
._index.css
._background-landscape.png
._background-portrait.png
._background.jpg
._baker-badge.png
._baker-framework-badge.png
._blood-drop.png
._blood.png
._cover-image.png
._dots-menu.png
._1-01-a.gif
._1-01-b.gif
._1-02-a.gif
._1-02-b.gif
._1-03-a.gif
._1-03-b.gif
._1-04-a.gif
._1-04-b.gif
._1-05-a.gif
._1-06-a.gif
._1-06-b.gif
._1-07-a.gif
._1-07-b.gif
._2-01-a.gif
._2-01-b.gif
._2-02-a.gif
._2-02-b.gif
._2-03-a.gif
._2-04-a.gif
._2-04-b.gif
._2-05-a.gif
._2-06-a.gif
._2-06-b.gif
._2-07-a.gif
._Book Cover.html
._Book Index.html
._book.json
._index.html
._Part1-01.html
._Part1-02.html
._Part1-03.html
._Part1-04.html
._Part1-05.html
._Part1-06.html
._Part1-07.html
._Part2-01.html
._Part2-02.html
._Part2-03.html
._Part2-04.html
._Part2-05.html
._Part2-06.html
._Part2-07.html
._Tail.html
._ios-icon-ipad.png
._ios-icon-ipad@2x.png
._ios-icon-iphone.png
._ios-icon-iphone@2x.png
OCMock.framework
._Info.plist
._License.txt
._Resources
._Versions
._.DS_Store
._.gitignore
._back.png
._back@2x.png
._Baker-Info.plist
._Baker.xcodeproj
._Baker_Prefix.pch
._forward.png
._forward@2x.png
._libOCMock.a
._OCMock.framework
._README.md
._UnitTests-Info.plist
._germanocaldeira-baker
SSZipArchive.m
SSZipArchive
Created by Sam Soffes on 7/21/10.
Copyright Sam Soffes 2010. All rights reserved.
#import &SSZipArchive.h&
#include &minizip/zip.h&
#include &minizip/unzip.h&
#import &zlib.h&
#import &zconf.h&
@interface SSZipArchive (PrivateMethods)
+ (NSDate *)_dateFor1980;
@implementation SSZipArchive
+ (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination {
return [self unzipFileAtPath:path toDestination:destination overwrite:YES password:nil error:nil];
+ (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination overwrite:(BOOL)overwrite password:(NSString *)password error:(NSError **)error {
// Begin opening
zipFile zip = unzOpen((const char*)[path UTF8String]);
if (zip == NULL) {
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@&failed to open zip file& forKey:NSLocalizedDescriptionKey];
if (error) {
*error = [NSError errorWithDomain:@&SSZipArchiveErrorDomain& code:-1 userInfo:userInfo];
return NO;
unz_global_info
globalInfo = {0};
unzGetGlobalInfo(zip, &globalInfo);
// Begin unzipping
if (unzGoToFirstFile(zip) != UNZ_OK) {
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@&failed to open first file in zip file& forKey:NSLocalizedDescriptionKey];
if (error) {
*error = [NSError errorWithDomain:@&SSZipArchiveErrorDomain& code:-2 userInfo:userInfo];
return NO;
BOOL success = YES;
unsigned char buffer[4096] = {0};
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDate *nineteenEighty = [self _dateFor1980];
if ([password length] == 0) {
ret = unzOpenCurrentFile(zip);
ret = unzOpenCurrentFilePassword(zip, [password cStringUsingEncoding:NSASCIIStringEncoding]);
if (ret != UNZ_OK) {
success = NO;
// Reading data and write to file
unz_file_info fileInfo = {0};
ret = unzGetCurrentFileInfo(zip, &fileInfo, NULL, 0, NULL, 0, NULL, 0);
if (ret != UNZ_OK) {
success = NO;
unzCloseCurrentFile(zip);
char *filename = (char *)malloc(fileInfo.size_filename + 1);
unzGetCurrentFileInfo(zip, &fileInfo, filename, fileInfo.size_filename + 1, NULL, 0, NULL, 0);
filename[fileInfo.size_filename] = '\0';
// Check if it contains directory
NSString *strPath = [NSString stringWithCString:filename encoding:NSUTF8StringEncoding];
BOOL isDirectory = NO;
if (filename[fileInfo.size_filename-1] == '/' || filename[fileInfo.size_filename-1] == '\\') {
isDirectory = YES;
free(filename);
// Contains a path
if ([strPath rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@&/\\&]].location != NSNotFound) {
strPath = [strPath stringByReplacingOccurrencesOfString:@&\\& withString:@&/&];
NSString* fullPath = [destination stringByAppendingPathComponent:strPath];
if (isDirectory) {
[fileManager createDirectoryAtPath:fullPath withIntermediateDirectories:YES attributes:nil error:nil];
[fileManager createDirectoryAtPath:[fullPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];
if ([fileManager fileExistsAtPath:fullPath] && !isDirectory && !overwrite) {
unzCloseCurrentFile(zip);
ret = unzGoToNextFile(zip);
FILE *fp = fopen((const char*)[fullPath UTF8String], &wb&);
while (fp) {
read = unzReadCurrentFile(zip, buffer, 4096);
if (read & 0) {
fwrite(buffer, read, 1, fp );
fclose(fp);
// Set the orignal datetime property
if (fileInfo.dosDate != 0) {
NSDate *orgDate = [[NSDate alloc] initWithTimeInterval:(NSTimeInterval)fileInfo.dosDate
sinceDate:nineteenEighty];
NSDictionary *attr = [NSDictionary dictionaryWithObject:orgDate forKey:NSFileModificationDate];
if (attr) {
if ([fileManager setAttributes:attr ofItemAtPath:fullPath error:nil] == NO) {
// Can't set attributes
NSLog(@&Failed to set attributes&);
[orgDate release];
unzCloseCurrentFile( zip );
ret = unzGoToNextFile( zip );
} while(ret == UNZ_OK && UNZ_OK != UNZ_END_OF_LIST_OF_FILE);
unzClose(zip);
+ (NSDate *)_dateFor1980 {
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:1];
[comps setMonth:1];
[comps setYear:1980];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:comps];
[comps release];
[gregorian release];
Copyright(C)
OKBASE.NET All Rights Reserved 好库网 版权所有SSZipArchive 与 ZipArchive - 博客频道 - CSDN.NET
摄影、编程、旅行
分类:iOS_工具类
SSZipArchive 与 ZipArchive 用法类似
SSZipArchive/ZipArchive功能:
解压zip文件
解压密码保护的zip文件
创建zip文件
追加到zip文件
使用一个名字来压缩NSData
把SSZipArchive/ZipArchive文件添加到项目中
在target中添加libz.dylib
SSZipArchive 使用方法:[oc]
NSString *zipPath = @"被解压的文件路径";
NSString *destinationPath = @"解压到的目录";
[SSZipArchive unzipFileAtPath:zipPath toDestination:destinationPath];
NSString *zippedPath = @"压缩文件路径";
NSArray *inputPaths = [NSArray arrayWithObjects: [[NSBundle mainBundle] pathForResource:@"photo1" ofType:@"jpg"],[[NSBundle mainBundle] pathForResource:@"photo2" ofType:@"jpg"],nil];
[SSZipArchive createZipFileAtPath:zippedPath withFilesAtPaths:inputPaths];
ZipArchive 使用方法:[oc]
ZipArchive *zipAch = [[ZipArchive alloc] init];
NSString *resFilePath = @"被解压的文件路径";
NSString *desFilePath = @"解压到的目录";
if([zipAch UnzipOpenFile:resFilePath]){
BOOL ret = [zipAch UnzipFileTo: desFilePath overWrite: YES];
if (NO == ret){
[zipAch UnzipCloseFile];
return NO;
[zipAch UnzipCloseFile];
return YES;
压缩方法自行研究文档,不在赘述。
排名:千里之外
(0)(3)(2)(3)(1)(2)(3)(4)(1)(1)(4)(1)(1)(1)(4)(2)(1)Swift 使用SSZipArchive实现文件的压缩、解压缩代码
核心提示:通常我们为了节约流量,传输多个文件的时候需要将它们打包成Zip文件再传输,或者把下载下来的Zip包进行解压。本文介绍如何使用 Z
通常我们为了节约流量,传输多个文件的时候需要将它们打包成Zip文件再传输,或者把下载下来的Zip包进行解压。本文介绍如何使用 ZipArchive 进行文件的压缩、解压操作。
1,SSZipArchive介绍
SSZipArchive是一个使用Objective-C编写的在iOS、Mac下的压缩、解压缩工具类。
GitHub地址:/ZipArchive/ZipArchive
功能如下:
(1)解压zip文件
(2)解压带密码保护的zip文件
(3)创建zip文件
(4)添加新文件到zip文件中
(5)压缩文件
(6)使用一个名字来压缩NSData对象
2,SSZipArchive的安装配置
(1)将下载下来的 SSZipArchive 文件夹添加到项目中来
(2)创建桥接头文件 bridge.h 来包含需要引用的Objective-C头文件,内容如下:
#import &ZipArchive.h&
(3)在项目target -& Build Phases -& link Binary With Libraries中点击加号,添加 libz.dylib&
3,使用样例
首先为了便于后面测试,我们先在项目中添加两张图片,以及两个压缩包文件(其中 test_password.zip 是带密码的压缩包,密码是:)
同时定义一个方法返回目标路径(每次调用都会在程序的 Caches 下创建一个随机文件夹),为的是让每次压缩、解压缩的目标保存地址都不会冲突:
//在Caches文件夹下随机创建一个文件夹,并返回路径
func tempDestPath() -& String? {
&&& var path = NSSearchPathForDirectoriesInDomains(.CachesDirectory,
&&&&&&& .UserDomainMask, true)[0]
&&& path += &/\(NSUUID().UUIDString)&
&&& let url = NSURL(fileURLWithPath: path)
&&&&&&& try NSFileManager.defaultManager().createDirectoryAtURL(url,
&&&&&&&&&&& withIntermediateDirectories: true, attributes: nil)
&&& } catch {
&&&&&&& return nil
&&& if let path = url.path {
&&&&&&& print(&path:\(path)&)
&&&&&&& return path
&&& return nil
(1)解压普通zip文件
let zipPath&& = NSBundle.mainBundle().pathForResource(&test&, ofType: &zip&)
SSZipArchive.unzipFileAtPath(zipPath, toDestination: tempDestPath())
(2)解压带密码的zip文件
let zipPath2&& = NSBundle.mainBundle().pathForResource(&test_password&, ofType: &zip&)
&&& try SSZipArchive.unzipFileAtPath(zipPath2, toDestination: tempDestPath(),
&&&&&&& overwrite: true, password: &&)
(3)将文件打成压缩包
let files = [NSBundle.mainBundle().pathForResource(&logo&, ofType: &png&)!,
&&& NSBundle.mainBundle().pathForResource(&icon&, ofType: &png&)!]
let zipPath3 = tempDestPath()! + &/hangge.zip&
SSZipArchive.createZipFileAtPath(zipPath3, withFilesAtPaths: files)
当然我们也是可以给压缩包加上密码的:
SSZipArchive.createZipFileAtPath(zipPath3, withFilesAtPaths: files,
&&& withPassword: &&)
(4)将整个文件夹下的文件打成压缩包
//需要压缩的文件夹啊
let filePath:String = NSHomeDirectory() + &/documents&
//先在该文件夹下添加一个文件
let image = UIImage(named: &logo.png&)
let data:NSData = UIImagePNGRepresentation(image!)!
data.writeToFile(filePath + &/logo.png&, atomically: true)
let zipPath5 = tempDestPath()! + &/hangge.zip&
SSZipArchive.createZipFileAtPath(zipPath5, withContentsOfDirectory: filePath)
&& 同样的,我门也可以添加密码:
SSZipArchive.createZipFileAtPath(zipPath6, withContentsOfDirectory: filePath,
&&& withPassword: &&) //带密码
●【往下看,下面更精彩】●
下一篇:暂无& Download
Downloading the ZipArchive Library
Current Version:
By downloading and using the ZipArchive Library you agree to its license terms.
The licensing information accompanies the appropriate download section. Please read
it before downloading.
To be notified about the ZipArchive Library new releases, .
To access the Full Version of the ZipArchive Library, please
The ZipArchive Library Open Source (GNU GPL) Version
The open source version of the ZipArchive Library is licensed under . This type of licensing is usually not appropriate for proprietary
software, please visit the
page for the details.
Version: 4.6.3
Sample MFC Application - ZipArc
Sample STL Application - Zippie}

我要回帖

更多关于 ssziparchive怎么使用 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信