Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone Unzip code

Really stuck on trying to write code to unzip a file or directory on the iPhone.

Below is some sample code that I'm using to try and unzip a simple text file.

It unzips the file but its corrupt.

(void)loadView {      NSString *DOCUMENTS_FOLDER = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];     NSString *path = [DOCUMENTS_FOLDER stringByAppendingPathComponent:@"sample.zip"];      NSString *unzipeddest = [DOCUMENTS_FOLDER stringByAppendingPathComponent:@"test.txt"];        gzFile file = gzopen([path UTF8String], "rb");      FILE *dest = fopen([unzipeddest UTF8String], "w");      unsigned char buffer[CHUNK];      int uncompressedLength = gzread(file, buffer, CHUNK);      if(fwrite(buffer, 1, uncompressedLength, dest) != uncompressedLength ||     ferror(dest)) {         NSLog(@"error writing data");     }     else{      }      fclose(dest);     gzclose(file);   } 
like image 543
TonyNeallon Avatar asked Jan 05 '09 12:01

TonyNeallon


People also ask

Can iPhone extract zips?

How to open a ZIP file on your iPhone or iPod touch. Open the Files app, then find the ZIP file or archive from which you want to extract. Tap the ZIP file or archive. A folder is created containing the files.

How do I unzip a code?

Do one of the following: To unzip a single file or folder, open the zipped folder, then drag the file or folder from the zipped folder to a new location. To unzip all the contents of the zipped folder, press and hold (or right-click) the folder, select Extract All, and then follow the instructions.


1 Answers

I wanted an easy solution and didn't find one I liked here, so I modified a library to do what I wanted. You may find SSZipArchive useful. (It can also create zip files by the way.)

Usage:

NSString *path = @"path_to_your_zip_file"; NSString *destination = @"path_to_the_folder_where_you_want_it_unzipped"; [SSZipArchive unzipFileAtPath:path toDestination:destination]; 
like image 70
Sam Soffes Avatar answered Oct 14 '22 10:10

Sam Soffes