Zip and Unzip a File Programmatically in iOS

Zip and UnZip a file programmatically in iOS?

Take a look at this SO questions.

And here is a library ziparchive

ziparchive An Objective C class for
zip/unzip on iPhone and Mac OSX

Swift: unzipping file

ZIP Foundation supports accessing individual entries in ZIP archives.

You have to initialize an archive by passing a file URL to the Archive initializer.

Afterwards you can access a specific entry via subscripting:

let fileManager = FileManager()
let currentWorkingPath = fileManager.currentDirectoryPath
var archiveURL = URL(fileURLWithPath: currentWorkingPath)
archiveURL.appendPathComponent("test.zip")
guard let archive = Archive(url: archiveURL, accessMode: .read) else {
return
}
guard let entry = archive["file.txt"] else {
return
}
var destinationURL = URL(fileURLWithPath: currentWorkingPath)
destinationURL.appendPathComponent("out.txt")
do {
try archive.extract(entry, to: destinationURL)
} catch {
print("Extracting entry from archive failed with error:\(error)")
}

You can also directly access the contents of entry by using the closure based API. This allows you to process the entry without writing it to the file system first:

try archive.extract(entry, consumer: { (data) in
print(data.count)
})

Decompress a zip file with Swift

I recently released a Swift native framework that allows you to create, read and update ZIP archive files: ZIP Foundation.

It internally uses libcompression for great compression performance.

Unzipping a file is basically just one line:

try fileManager.unzipItem(at: sourceURL, to: destinationURL)

A full example with some context would look like this:

let fileManager = FileManager()
let currentWorkingPath = fileManager.currentDirectoryPath
var sourceURL = URL(fileURLWithPath: currentWorkingPath)
sourceURL.appendPathComponent("archive.zip")
var destinationURL = URL(fileURLWithPath: currentWorkingPath)
destinationURL.appendPathComponent("directory")
do {
try fileManager.createDirectory(at: destinationURL, withIntermediateDirectories: true, attributes: nil)
try fileManager.unzipItem(at: sourceURL, to: destinationURL)
} catch {
print("Extraction of ZIP archive failed with error:\(error)")
}

The README on GitHub contains more information. All public methods also have full documentation available via Xcode Quick Help.

I also wrote a blog post about the performance characteristics here.

Unzipping downloaded files in iOS

I've used ZipArchive with success in the past. It's pretty ligthweight and simple to use, supports password protection, multiple files inside a ZIP, as well as compress & decompress.

The basic usage is:

NSString *filepath = [[NSBundle mainBundle] pathForResource:@"ZipFileName" ofType:@"zip"];
ZipArchive *zipArchive = [[ZipArchive alloc] init];
[zipArchive UnzipOpenFile:filepath Password:@"xxxxxx"];
[zipArchive UnzipFileTo:{pathToDirectory} overWrite:YES];
[zipArchive UnzipCloseFile];
[zipArchive release];

more examples about this package here

I have also tried SSZipArchive in some projects.
Below line would unzip your zip file.

[SSZipArchive unzipFileAtPath:path toDestination:destination];

iPhone Unzip code

Has "sample.zip" really been created with gZip? The .zip extension usually is used for archives created by WinZip. Those can also be decompressed using zLib, but you'd have to parse the header and use other routines.

To check, have a look at the first two bytes of the file. If it is 'PK', it's WinZip, if it's 0x1F8B, it's gZip.

Because this is iPhone specific, have a look at this iPhone SDK forum discussion where miniZip is mentioned. It seems this can handle WinZip files.

But if it's really a WinZip file, you should have a look at the WinZip specification and try to parse the file yourself. It basically should be parsing some header values, seeking the compressed stream position and using zLib routines to decompress it.



Related Topics



Leave a reply



Submit