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.
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];
}
}
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.