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

后端 未结 4 2051
暖寄归人
暖寄归人 2020-11-27 06:13

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?

相关标签:
4条回答
  • 2020-11-27 06:23

    As Alex pointed out, I responded to this question by pointing out the NSData category contributed 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.

    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.

    0 讨论(0)
  • 2020-11-27 06:26

    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.

    RWendi

    0 讨论(0)
  • 2020-11-27 06:28

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

    Some examples from their documentation:

    Creating a Zip File:

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

    Adding a file to a zip file:

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

    Reading a file from a zip file:

    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];
    
    0 讨论(0)
  • 2020-11-27 06:39

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

    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 :-

    -(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

    0 讨论(0)
提交回复
热议问题