Создание zip-файлов в ObjectiveC для iPhone - PullRequest
1 голос
/ 29 января 2010

Возможно ли создавать файлы .zip в цели C?

Есть ли библиотеки или предложения?

Ответы [ 3 ]

5 голосов
/ 23 января 2012

сначала вы скачаете пример Objective-zip с http://code.google.com/p/objective-zip/downloads/list

в этом примере Найдите и скопируйте три папки Objective-Zip, MiniZip и ZLib и перетащите их в свой проект

импортировать два класса в вас .m класс "ZipFile.h" и "ZipWriteStream.h"

метод создания почтового индекса мой код: -

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

}

В вашей папке документов сохранены файлы .png и .txt, которые архивируются в папке библиотеки с помощью backup.zip я надеюсь, что это поможет

4 голосов
/ 29 января 2010

Прежде чем кто-то упомянет http://code.google.com/p/ziparchive/ .. Я оценил этот код, и он довольно ужасен. Я закончил тем, что использовал его для быстрого демо-взлома, но я бы никогда не использовал его в производстве. ZipKit http://bitbucket.org/kolpanic/zipkit/wiki/Home, кажется, в гораздо лучшей форме.

0 голосов
/ 07 марта 2013
NSString *stringPath1 = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]];
NSString *FileName=[stringPath1 stringByAppendingPathComponent:@"Your file name"];


NSString *stringPath=[stringPath1 stringByAppendingPathComponent:[@"Your file name" stringByAppendingFormat:@".zip"]];
NSArray *files = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:FileName error:&error];
ZipFile *zipFile = [[ZipFile alloc]initWithFileName:stringPath mode:ZipFileModeCreate];

for(int i = 0;i<files.count;i++){

    id myArrayElement = [files  objectAtIndex:i];
    NSLog(@"add %@", myArrayElement);

    NSString *path = [FileName 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];
    [streem writeData:data];
    [streem finishedWriting];
}

[zipFile close];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...