iOS 收藏夹ios-存储ios-第三方框架/SDK

IOS压缩文件与解压

2016-01-22  本文已影响2695人  FengxinLi

由于项目里面需要轨迹点经纬度太多,上传比较慢,所以用文件保存然后压缩之后再上传。显示的时候需要解压然后读取显示。

https://github.com/ZipArchive/ZipArchive

这个是第三方的压缩解压框架。

1压缩

这个是宏

#define doc_path(_fileOrFolder_) [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:_fileOrFolder_]

//保存内容的文件text文件

NSString *path = doc_path(@"pointfile.txt");

NSMutableString *setStr=[[NSMutableString alloc]init];

for (int i=0;i<[pointss count];i++) {

CLLocation *cllocation=pointss[i];

NSString *str=[NSString stringWithFormat:@"%.5f,%.5f\n",cllocation.coordinate.longitude,cllocation.coordinate.latitude];

[setStr appendString:str];

}

NSString *str=setStr;

[str  writeToFile:path  atomically:YES encoding:NSUTF8StringEncoding error:nil];//写内容到文件里面

NSString *zippath = doc_path(@"zipfile.zip");//创建压缩文件路径

ZipArchive *za = [[ZipArchive alloc] init];//第三方压缩类

[za CreateZipFile2:zippath];//创建压缩文件

[za addFileToZip:path newname:@"zipfilepoint.txt"];//增加内容到压缩文件

BOOL success = [za CloseZipFile2];//关闭

2解压

如果是解压的话看是本地解压,还是从服务器下载然后解压。我的是从服务器下载然后解压。下载之后保存到本地然后解压。

NSString *FilePath = doc_cache_path(@"guiji.zip");

NSString *getpath = doc_cache_path(@"fileguiji");//解压到这个路径下面

NSData *data = entity;//这是服务器返回的数据

[data writeToFile:FilePath atomically:YES];//然后写到FilePath目录下面

ZipArchive* zipFile = [[ZipArchive alloc] init];

[zipFile UnzipOpenFile:FilePath];

[zipFile UnzipFileTo:getpath overWrite:YES];

[zipFile UnzipCloseFile];

上一篇 下一篇

猜你喜欢

热点阅读