Revision: 2616
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at March 14, 2007 13:38 by tgunr
Initial Code
/*
When you want to save your document as a Zip file.
using
cocoadev.com
http://www.cocoadev.com/index.pl?UsingZipFilesExamples
Please change the scratch folder etc to appropriate ones.
*/
-(void)usageExample
{
// create sample data
NSString* text = @"test";
NSData* data = [text dataUsingEncoding:NSUTF8StringEncoding];
// Zip test 1
/*
NSData* convertedData = [self zip:data];
[convertedData writeToFile:[NSHomeDirectory() stringByAppendingPathComponent: @"Desktop/zip result"] atomically:YES];
*/
// Zip test 2
NSFileWrapper* zipwrap = [[[NSFileWrapper alloc] initDirectoryWithFileWrappers:NULL ] autorelease];
[zipwrap addRegularFileWithContents:data preferredFilename:@"data1" ];
[zipwrap addRegularFileWithContents:data preferredFilename:@"data2" ];
NSData* convertedData = [self zip:zipwrap];
[convertedData writeToFile:[NSHomeDirectory() stringByAppendingPathComponent: @"Desktop/zip result"] atomically:YES];
// Unzip
NSFileWrapper* wrapper = [self unzip:convertedData];
if( [wrapper isRegularFile] )
{
NSLog(@"this is a regular file wrapper");
}
else
{
NSLog(@"this is a directory wrapper");
NSDictionary* regularFileWrappers = [wrapper fileWrappers];
NSLog(@"contains %@", [[regularFileWrappers allKeys] description] );
}
[wrapper writeToFile:[NSHomeDirectory() stringByAppendingPathComponent: @"Desktop/unzip result"] atomically:YES updateFilenames:YES];
}
-(NSData*)zip:(id)raw //raw must be NSData or NSFileWrapper
{
NSString* scratchFolder =[NSHomeDirectory() stringByAppendingPathComponent: @"Desktop/"];
NSString* sourceFilename = @"data";
NSString* targetFilename = @"zipped data";
NSString* sourcePath =[scratchFolder stringByAppendingPathComponent: sourceFilename];
NSString* targetPath =[scratchFolder stringByAppendingPathComponent: targetFilename];
BOOL flag = NO;
if( [raw isKindOfClass:[NSData class]] )
flag = [raw writeToFile:sourcePath atomically:YES];
else if( [raw isKindOfClass:[NSFileWrapper class]] )
flag = [raw writeToFile:sourcePath atomically:YES updateFilenames:YES];
if( flag == NO )
{
NSLog(@"Fail to write.");
return NULL;
}
/* Assumes sourcePath and targetPath are both
valid, standardized paths. */
//----------------
// Create the zip task
NSTask * backupTask = [[NSTask alloc] init];
[backupTask setLaunchPath:@"/usr/bin/ditto"];
[backupTask setArguments:
[NSArray arrayWithObjects:@"-c", @"-k", @"-X", @"--rsrc",
sourcePath, targetPath, nil]];
// Launch it and wait for execution
[backupTask launch];
[backupTask waitUntilExit];
// Handle the task's termination status
if ([backupTask terminationStatus] != 0)
{
NSLog(@"Sorry, didn't work.");
return NULL;
}
// You *did* remember to wash behind your ears ...
// ... right?
[backupTask release];
NSData* convertedData = [[[NSData alloc] initWithContentsOfFile:targetPath] autorelease];
//delete scratch
[[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceDestroyOperation
source:scratchFolder
destination:@""
files:[NSArray arrayWithObjects:sourceFilename, targetFilename,NULL]
tag:NULL];
return convertedData;
}
-(NSFileWrapper*)unzip:(NSData*)zipData
{
NSString* scratchFolder =[NSHomeDirectory()
stringByAppendingPathComponent: @"Desktop/"];
NSString* sourceFilename = @"zipped data";
NSString* targetFilename = @"unzipped folder";
NSString* sourcePath =[scratchFolder stringByAppendingPathComponent: sourceFilename];
NSString* targetPath =[scratchFolder stringByAppendingPathComponent: targetFilename];
BOOL flag = [zipData writeToFile:sourcePath atomically:YES];
if( flag == NO )
{
NSLog(@"error");
return NULL;
}
//Unzip
//-------------------
NSTask *cmnd=[[NSTask alloc] init];
[cmnd setLaunchPath:@"/usr/bin/ditto"];
[cmnd setArguments:[NSArray arrayWithObjects:
@"-v",@"-x",@"-k",@"--rsrc",sourcePath,targetPath,nil]];
[cmnd launch];
[cmnd waitUntilExit];
// Handle the task's termination status
if ([cmnd terminationStatus] != 0)
{
NSLog(@"Sorry, didn't work.");
return NULL;
}
// You *did* remember to wash behind your ears ...
// ... right?
[cmnd release];
//unzip
//
NSArray* contents = [[NSFileManager defaultManager] directoryContentsAtPath:targetPath];
NSFileWrapper* wrapper;
if( [contents count] == 1 )
{
NSString* onepath;
onepath = [targetPath stringByAppendingPathComponent:[contents lastObject]];
NSData* data = [NSData dataWithContentsOfFile:onepath];
wrapper = [[[NSFileWrapper alloc] initRegularFileWithContents:data ] autorelease];
}
else if( [contents count] > 1 )
{
wrapper = [[[NSFileWrapper alloc] initDirectoryWithFileWrappers:NULL ] autorelease];
unsigned hoge;
for( hoge = 0; hoge < [contents count]; hoge++ )
{
NSString* onepath;
NSString* onefilename;
onefilename = [contents objectAtIndex:hoge];
onepath = [targetPath stringByAppendingPathComponent:onefilename];
NSData* data = [NSData dataWithContentsOfFile:onepath];
[wrapper addRegularFileWithContents:data preferredFilename:onefilename ];
}
}
//delete scratch
[[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceDestroyOperation
source:scratchFolder
destination:@""
files:[NSArray arrayWithObjects:sourceFilename, targetFilename,NULL]
tag:NULL];
return wrapper;
}
Initial URL
http://homepage.mac.com/mnishikata/page2/page2.html
Initial Description
Initial Title
Create Zip file from data which user can unarchive
Initial Tags
osx
Initial Language
Other