1

I am downloading a zipped folder using AFNetworking library, file is downloading but when i am trying to Un zip that zip folder, it is not opening. On mac machine it is also not extracting on double click and by Archive Utility.

NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
    NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
    return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
    NSLog(@"File downloaded to: %@", filePath);
    [SSZipArchive unzipFileAtPath:[filePath path] toDestination: [[filePath path] stringByDeletingLastPathComponent]];
}];
psk
  • 162
  • 1
  • 13
  • What kind of error throws in `SSZipArchive`? – CodeChanger Sep 07 '17 at 10:00
  • Set attributes failed for directory: 2017-09-07 10_06_53.zip/.storage. – psk Sep 07 '17 at 10:08
  • Error Domain=NSCocoaErrorDomain Code=4 "The file “.storage” doesn’t exist." UserInfo={NSFilePath=2017-09-07 10_06_53.zip/.storage, NSUnderlyingError=0x600000053ce0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}} – psk Sep 07 '17 at 10:09
  • what is ur destination path ? I think problem is there at destination path may be wrong. – CodeChanger Sep 07 '17 at 10:19
  • My source and destination path same, document directory. – psk Sep 07 '17 at 10:31

3 Answers3

1

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;

NSString *destinationPath = [NSString stringWithFormat:@"%@/filepath",basepath]];
[SSZipArchive basePath toDestination:destinationPath overwrite:NO password:nil error:nil];
Debasis
  • 11
  • 4
0

Instead of

[SSZipArchive unzipFileAtPath:[filePath path] toDestination: [[filePath path] stringByDeletingLastPathComponent]];

use:

[SSZipArchive unzipFileAtPath:zipPath toDestination:destinationPath overwrite:NO password:nil error:nil]

to unzip the file

CodeChanger
  • 7,953
  • 5
  • 49
  • 80
Debasis
  • 11
  • 4
0

try this

   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"vikas.sqlite"];
   NSString *outputPath = [paths objectAtIndex:0];
   [SSZipArchive unzipFileAtPath:path toDestination:outputPath delegate:self];

Unzip file will be on Output Path.

Vikas Rajput
  • 1,754
  • 1
  • 14
  • 26