code_junkie

iPhone Unzip code

Nov 13th, 2011
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. (void)loadView {
  2.  
  3. NSString *DOCUMENTS_FOLDER = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
  4. NSString *path = [DOCUMENTS_FOLDER stringByAppendingPathComponent:@"sample.zip"];
  5.  
  6. NSString *unzipeddest = [DOCUMENTS_FOLDER stringByAppendingPathComponent:@"test.txt"];
  7.  
  8. gzFile file = gzopen([path UTF8String], "rb");
  9.  
  10. FILE *dest = fopen([unzipeddest UTF8String], "w");
  11.  
  12. unsigned char buffer[CHUNK];
  13.  
  14. int uncompressedLength = gzread(file, buffer, CHUNK);
  15.  
  16. if(fwrite(buffer, 1, uncompressedLength, dest) != uncompressedLength || ferror(dest)) {
  17. NSLog(@"error writing data");
  18. }
  19. else{
  20.  
  21. }
  22.  
  23. fclose(dest);
  24. gzclose(file);
  25.  
  26. NSString *path = @"path_to_your_zip_file";
  27. NSString *destination = @"path_to_the_folder_where_you_want_it_unzipped";
  28. [SSZipArchive unzipFileAtPath:path toDestination:destination];
  29.  
  30. #import <zlib.h>
  31. #define CHUNK 16384
  32.  
  33.  
  34. NSLog(@"testing unzip of database");
  35. start = [NSDate date];
  36. NSString *zippedDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"foo.db.gz"];
  37. NSString *unzippedDBPath = [documentsDirectory stringByAppendingPathComponent:@"foo2.db"];
  38. gzFile file = gzopen([zippedDBPath UTF8String], "rb");
  39. FILE *dest = fopen([unzippedDBPath UTF8String], "w");
  40. unsigned char buffer[CHUNK];
  41. int uncompressedLength;
  42. while (uncompressedLength = gzread(file, buffer, CHUNK) ) {
  43. // got data out of our file
  44. if(fwrite(buffer, 1, uncompressedLength, dest) != uncompressedLength || ferror(dest)) {
  45. NSLog(@"error writing data");
  46. }
  47. }
  48. fclose(dest);
  49. gzclose(file);
  50. NSLog(@"Finished unzipping database");
  51.  
  52. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  53.  
  54. NSLog(@"document directory path:%@",paths);
  55.  
  56. self.documentDirectory = [paths objectAtIndex:0];
  57.  
  58. NSString *filePath = [NSString stringWithFormat:@"%@/abc", self.documentDirectory];
  59.  
  60. NSLog(@"file path is:%@",filePath);
  61.  
  62. NSString *fileContent = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"data.zip"];
  63.  
  64.  
  65. NSData *unzipData = [NSData dataWithContentsOfFile:fileContent];
  66.  
  67. [self.fileManager createFileAtPath:filePath contents:unzipData attributes:nil];
  68.  
  69. // here we go, unzipping code
  70.  
  71. ZipArchive *zipArchive = [[ZipArchive alloc] init];
  72.  
  73. if ([zipArchive UnzipOpenFile:filePath])
  74. {
  75. if ([zipArchive UnzipFileTo:self.documentDirectory overWrite:NO])
  76. {
  77. NSLog(@"Archive unzip success");
  78. [self.fileManager removeItemAtPath:filePath error:NULL];
  79. }
  80. else
  81. {
  82. NSLog(@"Failure to unzip archive");
  83. }
  84. }
  85. else
  86. {
  87. NSLog(@"Failure to open archive");
  88. }
  89. [zipArchive release];
  90.  
  91. NSArray *paths =
  92. NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  93.  
  94. NSString *documentsDirectory = [paths objectAtIndex:0];
  95.  
  96. NSString *saveLocation =
  97. [documentsDirectory stringByAppendingString:@"myfile.zip"];
  98.  
  99. NSFileManager* fileManager = [NSFileManager defaultManager];
  100.  
  101. if ([fileManager fileExistsAtPath:saveLocation]) {
  102. [fileManager removeItemAtPath:saveLocation error:nil];
  103.  
  104. }
  105.  
  106. NSURLRequest *theRequest =
  107. [NSURLRequest requestWithURL:
  108. [NSURL URLWithString:@"http://example.com/myfile.zip"]
  109. cachePolicy:NSURLRequestUseProtocolCachePolicy
  110. timeoutInterval:60.0];
  111.  
  112. NSData *received =
  113. [NSURLConnection sendSynchronousRequest:theRequest
  114. returningResponse:nil error:nil];
  115.  
  116. if ([received writeToFile:saveLocation atomically:TRUE]) {
  117. NSString *cmd =
  118. [NSString stringWithFormat:@"unzip "%@" -d"%@"",
  119. saveLocation, documentsDirectory];
  120.  
  121. // Here comes the magic...
  122. system([cmd UTF8String]);
  123. }
Add Comment
Please, Sign In to add comment