Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (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);
- NSString *path = @"path_to_your_zip_file";
- NSString *destination = @"path_to_the_folder_where_you_want_it_unzipped";
- [SSZipArchive unzipFileAtPath:path toDestination:destination];
- #import <zlib.h>
- #define CHUNK 16384
- NSLog(@"testing unzip of database");
- start = [NSDate date];
- NSString *zippedDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"foo.db.gz"];
- NSString *unzippedDBPath = [documentsDirectory stringByAppendingPathComponent:@"foo2.db"];
- gzFile file = gzopen([zippedDBPath UTF8String], "rb");
- FILE *dest = fopen([unzippedDBPath UTF8String], "w");
- unsigned char buffer[CHUNK];
- int uncompressedLength;
- while (uncompressedLength = gzread(file, buffer, CHUNK) ) {
- // got data out of our file
- if(fwrite(buffer, 1, uncompressedLength, dest) != uncompressedLength || ferror(dest)) {
- NSLog(@"error writing data");
- }
- }
- fclose(dest);
- gzclose(file);
- NSLog(@"Finished unzipping database");
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSLog(@"document directory path:%@",paths);
- self.documentDirectory = [paths objectAtIndex:0];
- NSString *filePath = [NSString stringWithFormat:@"%@/abc", self.documentDirectory];
- NSLog(@"file path is:%@",filePath);
- NSString *fileContent = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"data.zip"];
- NSData *unzipData = [NSData dataWithContentsOfFile:fileContent];
- [self.fileManager createFileAtPath:filePath contents:unzipData attributes:nil];
- // here we go, unzipping code
- ZipArchive *zipArchive = [[ZipArchive alloc] init];
- if ([zipArchive UnzipOpenFile:filePath])
- {
- if ([zipArchive UnzipFileTo:self.documentDirectory overWrite:NO])
- {
- NSLog(@"Archive unzip success");
- [self.fileManager removeItemAtPath:filePath error:NULL];
- }
- else
- {
- NSLog(@"Failure to unzip archive");
- }
- }
- else
- {
- NSLog(@"Failure to open archive");
- }
- [zipArchive release];
- NSArray *paths =
- NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentsDirectory = [paths objectAtIndex:0];
- NSString *saveLocation =
- [documentsDirectory stringByAppendingString:@"myfile.zip"];
- NSFileManager* fileManager = [NSFileManager defaultManager];
- if ([fileManager fileExistsAtPath:saveLocation]) {
- [fileManager removeItemAtPath:saveLocation error:nil];
- }
- NSURLRequest *theRequest =
- [NSURLRequest requestWithURL:
- [NSURL URLWithString:@"http://example.com/myfile.zip"]
- cachePolicy:NSURLRequestUseProtocolCachePolicy
- timeoutInterval:60.0];
- NSData *received =
- [NSURLConnection sendSynchronousRequest:theRequest
- returningResponse:nil error:nil];
- if ([received writeToFile:saveLocation atomically:TRUE]) {
- NSString *cmd =
- [NSString stringWithFormat:@"unzip "%@" -d"%@"",
- saveLocation, documentsDirectory];
- // Here comes the magic...
- system([cmd UTF8String]);
- }
Add Comment
Please, Sign In to add comment