1

I have kept my sqlite database in the S3 server in the .gz format. When my iOS App starts, I want to download the database in .gz file and decompress it in the documents directory.

Download part is working fine but decompression is not working.

I tried ZipArchive, but it doesn't decompress .gz file. It is able to unzip ZIP files. Below is the code, I tried.

 ZipArchive *za = [[ZipArchive alloc] init];

if ([za UnzipOpenFile:filePath]) {
    BOOL ret = [za UnzipFileTo:destPath overWrite:YES];
    [za UnzipCloseFile];
    [za release];

    if (ret == YES) {
        [self stopSpinner];

                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[appDelegate encTitle] message:@"Update successful.\nPlease restart the application for changes to take effect." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];
                [alert release];
            } else {
                //Didn't work
            }
        }

I found GZIP for decompression in iOS, but don't have any idea to use this. If anyone has idea, please share with me. Link for GZIP :: https://github.com/nicklockwood/GZIP

If anyone knows any other library for .gz decompression.....they are also welcome.

Anjali
  • 1,623
  • 5
  • 30
  • 50
  • Read this answer from Brad Larson: http://stackoverflow.com/questions/230984/compression-api-on-the-iphone/234099#234099 – Michal Jul 13 '15 at 09:34
  • This is an alternative easiest solution .. http://stackoverflow.com/questions/6786835/how-to-unzip-folder-in-ios-iphone-application – rejusss Jul 13 '15 at 09:40

2 Answers2

2

What specifically are you having difficulty with using the GZIP library?

All you need to do is call gunzippedData on an NSData instance and it will return a new NSData object with the unzipped data.

Update

The GZIP library does not work with files, however it works directly with instances of NSData. This means you will have to construct an NSData object from your compressed .gz file manually, uncompress it, and write the uncompressed data to the documents directory...

// assuming filePath is a valid local path to the .gz file
NSError *error = nil;

// Create an NSData instance from the file
NSData *compressedData = [NSData dataWithContentsOfFile:filePath options:0 error:&error];
if (!compressedData) {
    NSLog(@"Reading file failed: %@", error);
}
else {
    // Attempt to uncompress it
    NSData *uncompressedData = [compressedData gunzippedData];
    if (!uncompressedData) {
        NSLog(@"Decompression failed");
    }
    else {
        // Write the uncompressed data to disk
        // You will need to set pathToSQLiteFile to the desired location of the SQLite database
        [uncompressedData writeToFile:pathToSQLiteFile atomically:YES];
    }
}
Steve Wilford
  • 8,894
  • 5
  • 42
  • 66
  • I am very new to iOS, So don't have much idea about iOS code.....I am not able to find where and how to set the zipped file name and the destination path. – Anjali Jul 13 '15 at 10:12
  • I've added some example code that you should be able to use. – Steve Wilford Jul 13 '15 at 10:25
  • i am getting "class method +gunzippedData not found" error....eventhough i have imported GZIP.h file – Anjali Jul 13 '15 at 11:02
  • Oops, my apologies, I had incorrectly called [NSData gunzippedData], it should have been [compressedData gunzippedData]. I have updated the answer. – Steve Wilford Jul 13 '15 at 11:04
  • Thanks Steve....Thank you so much...you saved my lot of time...Now decompression works fine... – Anjali Jul 13 '15 at 11:14
  • Hey Steve...can you please help me in my next question...http://stackoverflow.com/questions/31385368/downloading-files-in-serial-order-using-nsurlconnection-in-ios – Anjali Jul 13 '15 at 13:57
0

I maintain a lightweight Swift 3+ wrapper library around Apple's own compression library called DataCompression. Among others it also supports the GZIP format.

Decompressing a .gz text file and printing the content would look like this:

import DataCompression

let compressedData = try? Data(contentsOf: URL(fileURLWithPath: "/path/to/your/file.txt.gz"))

if let uncompressedData = compressedData?.gunzip() {
    print(String(data: uncompressedData, encoding: .utf8) ?? "Can't decode UTF-8")
}

You may refer to the README for a complete overview of the interface and other compression formats.

LimeRed
  • 1,278
  • 13
  • 18