-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
TPFileSerialization.m
141 lines (106 loc) · 3.37 KB
/
TPFileSerialization.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
//
// TPFileSerialization.m
// Streams
//
// Created by Julien Robert on 09/10/09.
// Copyright 2009 Apple. All rights reserved.
//
#import "TPFileSerialization.h"
NSString * TPFileArchiverNameKey = @"TPFileArchiverName";
NSString * TPFileArchiverTypeKey = @"TPFileArchiverType";
@implementation TPFileArchiver
+ (NSDictionary*)environment
{
return @{@"LANG": @"en_US.UTF-8",
@"LC_CTYPE": @"en_US.UTF-8"};
}
- (instancetype) initForReadingFilesAtPaths:(NSArray*)paths delegate:(id<TPFileArchiverDelegate>)delegate
{
self = [super init];
_delegate = delegate;
_paths = paths;
_readPipe = [[NSPipe alloc] init];
_readTask = [[NSTask alloc] init];
[_readTask setLaunchPath:@"/usr/bin/tar"];
NSMutableArray * arguments = [NSMutableArray arrayWithObjects:@"cz", @"-", nil];
NSEnumerator * pathsEnum = [paths objectEnumerator];
NSString * commonParentPath = nil;
NSString * path;
while((path = [pathsEnum nextObject]) != nil) {
if(commonParentPath == nil) {
commonParentPath = [path stringByDeletingLastPathComponent];
}
else if(![commonParentPath isEqual:[path stringByDeletingLastPathComponent]]) {
continue;
}
[arguments addObject:[path lastPathComponent]];
}
[_readTask setArguments:arguments];
[_readTask setEnvironment:[TPFileArchiver environment]];
[_readTask setCurrentDirectoryPath:commonParentPath];
[_readTask setStandardOutput:_readPipe];
_readFileHandle = [_readPipe fileHandleForReading];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fileHandleDidReadData:) name:NSFileHandleReadCompletionNotification object:_readFileHandle];
return self;
}
- (NSArray*)representedFiles
{
NSMutableArray * representedFiles = [NSMutableArray array];
NSEnumerator * pathsEnum = [_paths objectEnumerator];
NSString * path;
while((path = [pathsEnum nextObject]) != nil) {
NSDictionary * fileDict = [[NSDictionary alloc] initWithObjectsAndKeys:
[path lastPathComponent], TPFileArchiverNameKey,
[[NSWorkspace sharedWorkspace] typeOfFile:path error:NULL], TPFileArchiverTypeKey,
nil];
[representedFiles addObject:fileDict];
}
return representedFiles;
}
- (void)startReading
{
[_readTask launch];
[_readFileHandle readInBackgroundAndNotify];
}
- (void)continueReading
{
[_readFileHandle readInBackgroundAndNotify];
}
- (void)fileHandleDidReadData:(NSNotification*)notification
{
NSData * data = [notification userInfo][NSFileHandleNotificationDataItem];
if([data length] > 0) {
[_delegate fileArchiver:self hasDataAvailable:data];
}
else {
[_delegate fileArchiverDidCompleteReading:self];
}
}
@end
@implementation TPFileUnarchiver
- (instancetype) initForWritingAtPath:(NSString*)path
{
self = [super init];
_writePipe = [[NSPipe alloc] init];
_writeTask = [[NSTask alloc] init];
[_writeTask setLaunchPath:@"/usr/bin/tar"];
[_writeTask setArguments:@[@"xz", @"-C", path]];
[_writeTask setEnvironment:[TPFileArchiver environment]];
[_writeTask setStandardInput:_writePipe];
_writeFileHandle = [_writePipe fileHandleForWriting];
return self;
}
- (void)startWriting
{
[_writeTask launch];
}
- (void)close
{
[_writeFileHandle closeFile];
[_writeTask waitUntilExit];
}
- (void)writeData:(NSData*)data
{
[_writeFileHandle writeData:data];
}
@end