ios 如何使用 Objective C 创建 zip 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/286496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:23:23  来源:igfitidea点击:

How can I create a zip file by using Objective C?

objective-cioscocoa-touch

提问by issac

I am developing an iOS application, and trying to zip the file I have created in the application, is there any built in function able to do this?

我正在开发一个 iOS 应用程序,并试图压缩我在应用程序中创建的文件,是否有任何内置功能能够做到这一点?

采纳答案by Brad Larson

As Alex pointed out, I responded to this questionby pointing out the NSData categorycontributed by users of the Cocoadev wiki. That category includes methods for dealing with zipped and gzipped data in an NSData instance (that could be read from a Zip file or written to one). This should be all you need to implement the file zipping you describe, as long as you can feed your file data into an NSData instance.

正如 Alex 所指出的,我通过指出Cocoadev wiki 用户贡献的NSData 类别来回答这个问题。该类别包括处理 NSData 实例中的压缩和 gzip 数据的方法(可以从 Zip 文件中读取或写入一个文件)。只要您可以将文件数据提供给 NSData 实例,这应该就是您实现所描述的文件压缩所需的全部内容。

For an example of this category in action, please see the source code for my iPhone application, Molecules. I only use the method to extract data from a gzipped file (in SLSMolecule+PDB.m), but you should be able to get the basic concepts from that.

有关此类别的示例,请参阅我的 iPhone 应用程序Molecules的源代码。我只使用该方法从 gzipped 文件中提取数据(在 SLSMolecule+PDB.m 中),但您应该能够从中获得基本概念。

回答by Klaas

I definitely recommend Objective-Zip. It just recently moved to https://github.com/flyingdolphinstudio/Objective-Zip:

我绝对推荐Objective-Zip。它最近才移到https://github.com/flyingdolphinstudio/Objective-Zip

Some examples from their documentation:

他们文档中的一些示例:

Creating a Zip File:

创建 Zip 文件:

ZipFile *zipFile= [[ZipFile alloc] initWithFileName:@"test.zip" mode:ZipFileModeCreate];

Adding a file to a zip file:

将文件添加到 zip 文件:

ZipWriteStream *stream= [zipFile writeFileInZipWithName:@"abc.txt" compressionLevel:ZipCompressionLevelBest];

[stream writeData:abcData];
[stream finishedWriting];

Reading a file from a zip file:

从 zip 文件中读取文件:

ZipFile *unzipFile= [[ZipFile alloc] initWithFileName:@"test.zip" mode:ZipFileModeUnzip];

[unzipFile goToFirstFileInZip];

ZipReadStream *read= [unzipFile readCurrentFileInZip];
NSMutableData *data= [[NSMutableData alloc] initWithLength:256];
int bytesRead= [read readDataWithBuffer:data];

[read finishedReading];

回答by nlg

first you download Objective-zip example from http://code.google.com/p/objective-zip/downloads/list

首先从http://code.google.com/p/objective-zip/downloads/list下载 Objective-zip 示例

in this example Find and copy three folder Objective-Zip, MiniZip and ZLib drag in to your project

import two class in you .m class "ZipFile.h" and "ZipWriteStream.h"

create method of zip my code is :-

在本例中查找并复制三个文件夹 Objective-Zip、MiniZip 和 ZLib 拖入您的项目中

在你的.m 类中导入两个类“ZipFile.h”和“ZipWriteStream.h”

zip 我的代码的创建方法是:-

-(IBAction)Zip{
self.fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory , NSUserDomainMask, YES);

NSString *ZipLibrary = [paths objectAtIndex:0];


NSString *fullPathToFile = [ZipLibrary stringByAppendingPathComponent:@"backUp.zip"];

//[self.fileManager createDirectoryAtPath:fullPathToFile attributes:nil];

//self.documentsDir = [paths objectAtIndex:0];


ZipFile *zipFile = [[ZipFile alloc]initWithFileName:fullPathToFile mode:ZipFileModeCreate];

NSError *error = nil;
self.fileManager = [NSFileManager defaultManager];
NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);

self.documentsDir = [paths1 objectAtIndex:0];
NSArray *files = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:self.documentsDir error:&error];
//for(NSString *filename in files){
    for(int i = 0;i<files.count;i++){

        id myArrayElement = [files  objectAtIndex:i];


    if([myArrayElement rangeOfString:@".png" ].location !=NSNotFound){
            NSLog(@"add %@", myArrayElement);


    NSString *path = [self.documentsDir stringByAppendingPathComponent:myArrayElement];
    NSDictionary *attributes = [[NSFileManager defaultManager]attributesOfItemAtPath:path error:&error];
    NSDate *Date = [attributes objectForKey:NSFileCreationDate];

    ZipWriteStream *streem = [zipFile writeFileInZipWithName:myArrayElement fileDate:Date compressionLevel:ZipCompressionLevelBest];
    NSData *data = [NSData dataWithContentsOfFile:path];
    //  NSLog(@"%d",data);
    [streem writeData:data];
    [streem finishedWriting];
        }else if([myArrayElement rangeOfString:@".txt" ].location !=NSNotFound)
        {

            NSString *path = [self.documentsDir stringByAppendingPathComponent:myArrayElement];
            NSDictionary *attributes = [[NSFileManager defaultManager]attributesOfItemAtPath:path error:&error];
            NSDate *Date = [attributes objectForKey:NSFileCreationDate];

            ZipWriteStream *streem = [zipFile writeFileInZipWithName:myArrayElement fileDate:Date compressionLevel:ZipCompressionLevelBest];
            NSData *data = [NSData dataWithContentsOfFile:path];
            //  NSLog(@"%d",data);
            [streem writeData:data];
            [streem finishedWriting];
    }
}

[self testcsv];
[zipFile close];

}

your documents directory saved item .png and .txt files zipping in Library folder with backup.zip i hope this is helps

您的文档目录保存了项目 .png 和 .txt 文件,使用 backup.zip 压缩在库文件夹中,我希望这会有所帮助

回答by RWendi

Not sure if there's a built in function, but perhaps you could use an external library such as InfoZip. Or alternatively you could try implementing your own zip library, I am pretty sure Zip format specification can be found somewhere on the internet.

不确定是否有内置函数,但也许您可以使用外部库,例如InfoZip。或者,您可以尝试实现自己的 zip 库,我很确定可以在 Internet 上的某个地方找到 Zip 格式规范。

RWendi

温迪