-
Notifications
You must be signed in to change notification settings - Fork 17
/
MVZipFiles.m
executable file
·69 lines (58 loc) · 2.44 KB
/
MVZipFiles.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
//
// MVZipFiles.m
// FileShuttle
//
// Created by Michael Villar on 12/11/11.
//
#import "MVZipFiles.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation MVZipFiles
///////////////////////////////////////////////////////////////////////////////////////////////////
- (NSString*)zipFiles:(NSArray*)filenames
{
NSString *tmpDirectory = @"/tmp/fileshuttle_files/";
BOOL isDirectory;
if([[NSFileManager defaultManager] fileExistsAtPath:tmpDirectory
isDirectory:&isDirectory])
{
[[NSFileManager defaultManager] removeItemAtPath:tmpDirectory error:nil];
}
[[NSFileManager defaultManager] createDirectoryAtPath:tmpDirectory
withIntermediateDirectories:YES
attributes:nil
error:nil];
NSString *path = @"/tmp/files.zip";
if([[NSFileManager defaultManager] fileExistsAtPath:path])
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
NSTask *zipTask = [[NSTask alloc] init];
[zipTask setCurrentDirectoryPath:tmpDirectory];
[zipTask setLaunchPath:@"/usr/bin/zip"];
NSMutableArray *args = [NSMutableArray arrayWithObjects:@"-q", @"-r", @"-b", @".",
path,
nil];
NSString *file;
for(file in filenames) {
NSString *tmpFile = [NSString stringWithFormat:
@"%@%@",tmpDirectory,[file lastPathComponent]];
[[NSFileManager defaultManager] createSymbolicLinkAtPath:tmpFile
withDestinationPath:file
error:nil];
[args addObject:[file lastPathComponent]];
}
[zipTask setArguments:args];
// launch it and wait for execution
[zipTask launch];
[zipTask waitUntilExit];
// handle the task's termination status
if ([zipTask terminationStatus] == 0) {
[zipTask release];
NSDictionary *attributes=[NSDictionary dictionaryWithObject:[NSNumber numberWithShort:0644] forKey:NSFilePosixPermissions];
[[NSFileManager defaultManager] setAttributes:attributes ofItemAtPath:path error:nil];
return path;
}
[zipTask release];
return nil;
}
@end