Как заархивировать файл с помощью «библиотеки ZipArchive» в iphone? - PullRequest
1 голос
/ 23 сентября 2011

Я хочу реализовать ZipArchive в моем проекте.

Используя эту библиотеку, я хочу архивировать файлы, например изображения, в zip-файл.

Как это сделать?

Ответы [ 2 ]

4 голосов
/ 23 сентября 2011

попробуй мой код Почтовый индекс:

    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* dPath = [paths objectAtIndex:0];
    NSString* txtfile = [dPath stringByAppendingPathComponent:@"test.txt"];
    NSString* zipfile = [dPath stringByAppendingPathComponent:@"test.zip"];
    ZipArchive* zip = [[ZipArchive alloc] init];
    BOOL ret = [zip CreateZipFile2:zipfile];
    ret = [zip addFileToZip:txtfile newname:@"test.txt"];//zip
    if( ![zip CloseZipFile2] )
    {
        zipfile = @"";
    }
    [zip release];
    NSLog(@"The file has been zipped");

Unzipcode:

    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* dPath = [paths objectAtIndex:0];

    NSString* zipfile = [dPath stringByAppendingPathComponent:@"test.zip"] ;
    NSString* unzipto = [dPath stringByAppendingPathComponent:@"test"] ;
    ZipArchive* zip = [[ZipArchive alloc] init];
    if([zip UnzipOpenFile:zipfile] )
    {
        BOOL ret = [zip UnzipFileTo:unzipto overWrite:YES];
        if(NO == ret)
        {
        }
        [zip UnzipCloseFile];
    }
    [zip release];
    NSLog(@"The file has been unzipped");
2 голосов
/ 23 сентября 2011

Этот код отлично подойдет для архивирования файла.

ZipArchive *zip = [[ZipArchive alloc] init];
if(![zip UnzipOpenFile:fileToZipPath]) {
//open file is there

            if ([zip CreateZipFile2:newZipFilePath overWrite:YES]) {

                //zipped successfully

                NSLog(@"Archive zip Success");

            } 

        } else  {

            NSLog(@"Failure To Zip Archive");

        }
      }

Разархивировать,

if([zip UnzipOpenFile:zipFilePath]) {

            //zip file is there

            if ([zip UnzipFileTo:newFilePath overWrite:YES]) {

                //unzipped successfully

                NSLog(@"Archive unzip Success");

                zipOpened = YES;

            } 

        } else  {

            NSLog(@"Failure To Open Archive");

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