Posted By

tgunr on 03/14/07


Tagged

osx objc


Versions (?)

Create Zip file from data which user can unarchive


 / Published in: Other
 

URL: http://homepage.mac.com/mnishikata/page2/page2.html

  1. /*
  2.  
  3.  
  4. When you want to save your document as a Zip file.
  5.  
  6. using
  7. cocoadev.com
  8. http://www.cocoadev.com/index.pl?UsingZipFilesExamples
  9.  
  10.  
  11. Please change the scratch folder etc to appropriate ones.
  12.  
  13. */
  14.  
  15.  
  16. -(void)usageExample
  17. {
  18. // create sample data
  19. NSString* text = @"test";
  20. NSData* data = [text dataUsingEncoding:NSUTF8StringEncoding];
  21.  
  22.  
  23.  
  24. // Zip test 1
  25.  
  26. /*
  27. NSData* convertedData = [self zip:data];
  28.  
  29. [convertedData writeToFile:[NSHomeDirectory() stringByAppendingPathComponent: @"Desktop/zip result"] atomically:YES];
  30.  
  31.  
  32. */
  33.  
  34. // Zip test 2
  35.  
  36. NSFileWrapper* zipwrap = [[[NSFileWrapper alloc] initDirectoryWithFileWrappers:NULL ] autorelease];
  37.  
  38. [zipwrap addRegularFileWithContents:data preferredFilename:@"data1" ];
  39. [zipwrap addRegularFileWithContents:data preferredFilename:@"data2" ];
  40.  
  41. NSData* convertedData = [self zip:zipwrap];
  42.  
  43. [convertedData writeToFile:[NSHomeDirectory() stringByAppendingPathComponent: @"Desktop/zip result"] atomically:YES];
  44.  
  45.  
  46.  
  47.  
  48. // Unzip
  49.  
  50. NSFileWrapper* wrapper = [self unzip:convertedData];
  51.  
  52. if( [wrapper isRegularFile] )
  53. {
  54. NSLog(@"this is a regular file wrapper");
  55.  
  56.  
  57. }
  58. else
  59. {
  60. NSLog(@"this is a directory wrapper");
  61.  
  62.  
  63. NSDictionary* regularFileWrappers = [wrapper fileWrappers];
  64.  
  65. NSLog(@"contains %@", [[regularFileWrappers allKeys] description] );
  66.  
  67. }
  68.  
  69. [wrapper writeToFile:[NSHomeDirectory() stringByAppendingPathComponent: @"Desktop/unzip result"] atomically:YES updateFilenames:YES];
  70.  
  71. }
  72.  
  73.  
  74.  
  75. -(NSData*)zip:(id)raw //raw must be NSData or NSFileWrapper
  76. {
  77. NSString* scratchFolder =[NSHomeDirectory() stringByAppendingPathComponent: @"Desktop/"];
  78.  
  79.  
  80. NSString* sourceFilename = @"data";
  81. NSString* targetFilename = @"zipped data";
  82.  
  83. NSString* sourcePath =[scratchFolder stringByAppendingPathComponent: sourceFilename];
  84. NSString* targetPath =[scratchFolder stringByAppendingPathComponent: targetFilename];
  85.  
  86.  
  87. BOOL flag = NO;
  88.  
  89.  
  90. if( [raw isKindOfClass:[NSData class]] )
  91. flag = [raw writeToFile:sourcePath atomically:YES];
  92.  
  93.  
  94. else if( [raw isKindOfClass:[NSFileWrapper class]] )
  95. flag = [raw writeToFile:sourcePath atomically:YES updateFilenames:YES];
  96.  
  97.  
  98. if( flag == NO )
  99. {
  100. NSLog(@"Fail to write.");
  101. return NULL;
  102. }
  103.  
  104. /* Assumes sourcePath and targetPath are both
  105. valid, standardized paths. */
  106.  
  107.  
  108.  
  109. //----------------
  110. // Create the zip task
  111. NSTask * backupTask = [[NSTask alloc] init];
  112. [backupTask setLaunchPath:@"/usr/bin/ditto"];
  113. [backupTask setArguments:
  114. [NSArray arrayWithObjects:@"-c", @"-k", @"-X", @"--rsrc",
  115. sourcePath, targetPath, nil]];
  116.  
  117. // Launch it and wait for execution
  118. [backupTask launch];
  119. [backupTask waitUntilExit];
  120.  
  121. // Handle the task's termination status
  122. if ([backupTask terminationStatus] != 0)
  123. {
  124. NSLog(@"Sorry, didn't work.");
  125. return NULL;
  126. }
  127.  
  128. // You *did* remember to wash behind your ears ...
  129. // ... right?
  130. [backupTask release];
  131.  
  132.  
  133. NSData* convertedData = [[[NSData alloc] initWithContentsOfFile:targetPath] autorelease];
  134.  
  135. //delete scratch
  136.  
  137. [[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceDestroyOperation
  138. source:scratchFolder
  139. destination:@""
  140. files:[NSArray arrayWithObjects:sourceFilename, targetFilename,NULL]
  141. tag:NULL];
  142.  
  143.  
  144. return convertedData;
  145. }
  146.  
  147.  
  148. -(NSFileWrapper*)unzip:(NSData*)zipData
  149. {
  150. NSString* scratchFolder =[NSHomeDirectory()
  151. stringByAppendingPathComponent: @"Desktop/"];
  152.  
  153. NSString* sourceFilename = @"zipped data";
  154. NSString* targetFilename = @"unzipped folder";
  155.  
  156. NSString* sourcePath =[scratchFolder stringByAppendingPathComponent: sourceFilename];
  157. NSString* targetPath =[scratchFolder stringByAppendingPathComponent: targetFilename];
  158.  
  159.  
  160.  
  161. BOOL flag = [zipData writeToFile:sourcePath atomically:YES];
  162.  
  163. if( flag == NO )
  164. {
  165. NSLog(@"error");
  166. return NULL;
  167. }
  168.  
  169.  
  170. //Unzip
  171.  
  172. //-------------------
  173. NSTask *cmnd=[[NSTask alloc] init];
  174. [cmnd setLaunchPath:@"/usr/bin/ditto"];
  175. [cmnd setArguments:[NSArray arrayWithObjects:
  176. @"-v",@"-x",@"-k",@"--rsrc",sourcePath,targetPath,nil]];
  177. [cmnd launch];
  178. [cmnd waitUntilExit];
  179.  
  180. // Handle the task's termination status
  181. if ([cmnd terminationStatus] != 0)
  182. {
  183. NSLog(@"Sorry, didn't work.");
  184. return NULL;
  185. }
  186.  
  187. // You *did* remember to wash behind your ears ...
  188. // ... right?
  189. [cmnd release];
  190.  
  191.  
  192.  
  193. //unzip
  194. //
  195.  
  196. NSArray* contents = [[NSFileManager defaultManager] directoryContentsAtPath:targetPath];
  197.  
  198.  
  199. NSFileWrapper* wrapper;
  200.  
  201. if( [contents count] == 1 )
  202. {
  203. NSString* onepath;
  204. onepath = [targetPath stringByAppendingPathComponent:[contents lastObject]];
  205.  
  206. NSData* data = [NSData dataWithContentsOfFile:onepath];
  207.  
  208. wrapper = [[[NSFileWrapper alloc] initRegularFileWithContents:data ] autorelease];
  209.  
  210.  
  211. }
  212. else if( [contents count] > 1 )
  213. {
  214.  
  215. wrapper = [[[NSFileWrapper alloc] initDirectoryWithFileWrappers:NULL ] autorelease];
  216.  
  217. unsigned hoge;
  218. for( hoge = 0; hoge < [contents count]; hoge++ )
  219. {
  220. NSString* onepath;
  221. NSString* onefilename;
  222.  
  223. onefilename = [contents objectAtIndex:hoge];
  224. onepath = [targetPath stringByAppendingPathComponent:onefilename];
  225.  
  226. NSData* data = [NSData dataWithContentsOfFile:onepath];
  227.  
  228. [wrapper addRegularFileWithContents:data preferredFilename:onefilename ];
  229. }
  230. }
  231.  
  232.  
  233.  
  234. //delete scratch
  235.  
  236. [[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceDestroyOperation
  237. source:scratchFolder
  238. destination:@""
  239. files:[NSArray arrayWithObjects:sourceFilename, targetFilename,NULL]
  240. tag:NULL];
  241.  
  242.  
  243. return wrapper;
  244. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: the_coder on December 9, 2010

[[[NSData alloc] initWithContentsOfFile:targetPath] autorelease] can be written as [NSData dataWithContentsOfFile:targetPath]

You need to login to post a comment.