Ziparchive not working with iphone device

184 Views Asked by At

I am generating zip file of my data which is in document directory, the problem is that Its working well with iphone simulator but same coding is not working for iphone actual device which is phone 5

Here is my code snippet

ZipArchive *archiver = [[ZipArchive alloc] init];
[archiver CreateZipFile2:archivePath];
for(NSString *path in subpaths)
{
    NSString *longPath = [exportPath stringByAppendingPathComponent:path];
    if([fileManager fileExistsAtPath:longPath isDirectory:&isDir] && !isDir)
    {
        [archiver CreateZipFile2:longPath];
        [archiver addFileToZip:longPath newname:path];
    }
}
BOOL successCompressing = [archiver CloseZipFile2];

I tried to googled but its not giving any help Please guide me Thanks

2

There are 2 best solutions below

0
On

Try this one:

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


     NSString *toCompress = @"AccReport1.html";
    NSString *pathToCompress = [documentsDirectory stringByAppendingPathComponent:toCompress];

    NSFileManager *fileManager = [NSFileManager defaultManager];



    NSString *zipFilePath = [documentsDirectory stringByAppendingPathComponent:@"myZipFileName.zip"];

    ZipArchive *za = [[ZipArchive alloc] init];
    [za CreateZipFile2:zipFilePath];  

    [za addFileToZip:pathToCompress newname:toCompress];
    [za release];

Make sure you have write this line:NSFileManager *fileManager = [NSFileManager defaultManager];

0
On

Hello try with below code:

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");