-
Notifications
You must be signed in to change notification settings - Fork 11
/
TSZip.m
146 lines (113 loc) · 4.38 KB
/
TSZip.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//
// TSZip.m
// ThisService
//
// Created by Jesper on 2007-04-23.
// Copyright 2007-2012 waffle software. All rights reserved.
// BSD licensed - see license.txt for more information.
//
#import "TSZip.h"
@implementation TSZip
/*
When you want to save your document as a Zip file.
using
cocoadev.com
http://www.cocoadev.com/index.pl?UsingZipFilesExamples
*/
static BOOL logging = NO;
#define TSZLog(...) do { if (logging) { NSLog(__VA_ARGS__); } } while (0)
+ (void)cleanseFileWrapperOfIcons:(NSFileWrapper *)fw {
if (nil != [fw icon]) [fw setIcon:nil];
if (nil != [fw fileWrappers] || [[fw fileWrappers] count] == 0) return;
NSDictionary *wrappers = [fw fileWrappers];
NSEnumerator *fwEnumerator = [wrappers keyEnumerator];
NSString *fwN;
while (fwN = [fwEnumerator nextObject]) {
NSFileWrapper *innerFW = [[fw fileWrappers] objectForKey:fwN];
[self cleanseFileWrapperOfIcons:innerFW];
}
}
+ (NSData *)zip:(id)raw //raw must be NSData or NSFileWrapper
{
TSZLog(@"ThisService zipping...");
CFUUIDRef uuidcf = CFUUIDCreate(NULL);
NSString *uuid = (NSString *)CFUUIDCreateString(NULL, uuidcf);
CFRelease(uuidcf);
NSString *scratchFolder = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"thisservice-zip-%@", uuid]];
[uuid release];
TSZLog(@"scratch folder: %@", scratchFolder);
NSFileManager *fm = [NSFileManager defaultManager];
NSError *err;
if (![fm createDirectoryAtPath:scratchFolder withIntermediateDirectories:NO attributes:nil error:&err]) {
TSZLog(@"could not create scratch folder at %@ because of %@", scratchFolder, err);
return nil;
}
TSZLog(@"created scratch folder");
NSString *sourceFilename = @"data";
NSString *targetFilename = @"zipped data";
NSString *sourcePath = [scratchFolder stringByAppendingPathComponent:sourceFilename];
NSString *targetPath = [scratchFolder stringByAppendingPathComponent:targetFilename];
TSZLog(@"source path: %@, target path: %@", sourcePath, targetPath);
BOOL flag = NO;
if([raw isKindOfClass:[NSData class]]) {
TSZLog(@"is data");
flag = [raw writeToFile:sourcePath atomically:YES];
}
else if([raw isKindOfClass:[NSFileWrapper class]]) {
TSZLog(@"is filewrapper");
NSFileWrapper *fw = raw;
//NSLog(@"fileWrappers: %@", [fw fileWrappers]);
NSArray *files = [[fw fileWrappers] allKeys];
TSZLog(@"files: %@", files);
[self cleanseFileWrapperOfIcons:fw];
//NSLog(@"cleansed: %@", fw);
flag = [raw writeToFile:sourcePath atomically:YES updateFilenames:YES];
TSZLog(@"wrote to file");
NSEnumerator *fileEnumerator = [files objectEnumerator];
NSString *file;
while (file = [fileEnumerator nextObject]) {
NSString *pathToGeneratedSuperfluosIcon = [[sourcePath stringByAppendingPathComponent:file] stringByAppendingPathExtension:@"tiff"];
if ([fm fileExistsAtPath:pathToGeneratedSuperfluosIcon]) {
TSZLog(@"horrible NSFileWrapper-generated icon exists for %@, let's remove it", file);
[fm removeItemAtPath:pathToGeneratedSuperfluosIcon error:NULL];
}
}
}
if(flag == NO) {
TSZLog(@"could not write file when zipping");
return nil;
}
/* Assumes sourcePath and targetPath are both
valid, standardized paths. */
//----------------
// Create the zip task
TSZLog(@"starting zip task");
NSTask * backupTask = [[NSTask alloc] init];
[backupTask setLaunchPath:@"/usr/bin/ditto"];
[backupTask setArguments:
[NSArray arrayWithObjects:@"-c", @"-k", @"-X", @"--sequesterRsrc",
sourcePath, targetPath, nil]];
TSZLog(@"launching");
// Launch it and wait for execution
[backupTask launch];
TSZLog(@"launched");
[backupTask waitUntilExit];
int terminationStatus = [backupTask terminationStatus];
[backupTask release];
TSZLog(@"exited, termstatus: %d", terminationStatus);
// Handle the task's termination status
if (terminationStatus != 0) {
TSZLog(@"Sorry, didn't work.");
return nil;
}
NSData *convertedData = [[NSData alloc] initWithContentsOfFile:targetPath];
TSZLog(@"created converted data");
//delete scratch
TSZLog(@"deleting scratch folders");
NSError *removeScratchFolderError = nil;
TSZLog(@"scratch folder: %@", scratchFolder);
[[NSFileManager defaultManager] removeItemAtPath:scratchFolder error:&removeScratchFolderError];
TSZLog(@"remove scratch folder: %@ error %@", scratchFolder, removeScratchFolderError);
return [convertedData autorelease];
}
@end