| 1 |
/* |
| 2 |
* Copyright (c) 2004-2005 The Lupin Project. All rights reserved. |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without modification, are permitted |
| 5 |
* provided that the following conditions are met: |
| 6 |
* |
| 7 |
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions |
| 8 |
* and the following disclaimer. |
| 9 |
* |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of |
| 11 |
* conditions and the following disclaimer in the documentation and/or other materials provided |
| 12 |
* with the distribution. |
| 13 |
* |
| 14 |
* THIS SOFTWARE IS PROVIDED BY THE LUPIN PROJECT ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 15 |
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LUPIN PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 17 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 18 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 19 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 20 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 21 |
* POSSIBILITY OF SUCH DAMAGE. |
| 22 |
*/ |
| 23 |
|
| 24 |
#import "ZipPlugin.h" |
| 25 |
|
| 26 |
|
| 27 |
@implementation ZipPlugin |
| 28 |
|
| 29 |
+ (id)alloc { |
| 30 |
return [super alloc]; |
| 31 |
} |
| 32 |
|
| 33 |
- (void)dealloc |
| 34 |
{ |
| 35 |
if (filePath) |
| 36 |
[filePath release]; |
| 37 |
[super dealloc]; |
| 38 |
} |
| 39 |
|
| 40 |
- (NSArray*)acceptableExtensions |
| 41 |
{ |
| 42 |
return [NSArray arrayWithObjects:@".zip",@".exe",nil]; |
| 43 |
} |
| 44 |
|
| 45 |
- (id)initWithFile:(NSString*)path |
| 46 |
{ |
| 47 |
self = [super init]; |
| 48 |
filePath = [NSString stringWithString:path]; |
| 49 |
[filePath retain]; |
| 50 |
_totalFileCount = _totalInArchiveFileCount = 0; |
| 51 |
return self; |
| 52 |
} |
| 53 |
|
| 54 |
#pragma mark Plugin Information |
| 55 |
|
| 56 |
- (NSString*)pluginName { |
| 57 |
return [[NSBundle bundleForClass:[self class]] localizedStringForKey:@"PluginName" value:@"Untitled Plugin" table:nil]; |
| 58 |
} |
| 59 |
- (NSString*)pluginAuthorName { |
| 60 |
return [[NSBundle bundleForClass:[self class]] localizedStringForKey:@"PluginAuthor" value:@"Unnamed Person" table:nil]; |
| 61 |
} |
| 62 |
|
| 63 |
- (NSString*)pluginVersion { |
| 64 |
return [[NSBundle bundleForClass:[self class]] localizedStringForKey:@"PluginVersion" value:@"ver.?" table:nil]; |
| 65 |
} |
| 66 |
|
| 67 |
- (NSString*)pluginURI { |
| 68 |
return [[NSBundle bundleForClass:[self class]] localizedStringForKey:@"PluginURI" value:@"" table:nil]; |
| 69 |
} |
| 70 |
- (NSString*)pluginEmail { |
| 71 |
return [[NSBundle bundleForClass:[self class]] localizedStringForKey:@"PluginEmail" value:@"" table:nil]; |
| 72 |
} |
| 73 |
|
| 74 |
- (NSAttributedString*)pluginCopyright { |
| 75 |
NSURL *url = [NSURL fileURLWithPath:[[NSBundle bundleForClass:[self class]] pathForResource:@"index" ofType:@"html"]]; |
| 76 |
NSData *data = [NSData dataWithContentsOfURL: url]; |
| 77 |
return [[NSAttributedString alloc] initWithHTML:data baseURL:url documentAttributes:(NSDictionary **)NULL]; |
| 78 |
} |
| 79 |
|
| 80 |
#pragma mark Archive Managing |
| 81 |
|
| 82 |
- (LPFile*)buildArchiveInfoDict:(BOOL)hierarchy password:(NSString*)password |
| 83 |
{ |
| 84 |
if (!filePath) { |
| 85 |
return NULL; |
| 86 |
} |
| 87 |
|
| 88 |
NSTask* pluginTask = [[[NSTask alloc] init] autorelease]; |
| 89 |
NSPipe* stdoutPipe = [NSPipe pipe]; |
| 90 |
NSPipe* stderrPipe = [NSPipe pipe]; |
| 91 |
//��������������������������������������������������������������������������������������������� |
| 92 |
NSString* curPath = [filePath stringByDeletingLastPathComponent]; |
| 93 |
|
| 94 |
// ������(���������)��������������� |
| 95 |
[pluginTask setStandardOutput:stdoutPipe]; |
| 96 |
[pluginTask setStandardError:stderrPipe]; |
| 97 |
|
| 98 |
// ������ |
| 99 |
[pluginTask setLaunchPath:[[[NSBundle bundleForClass:[self class]] resourcePath] stringByAppendingPathComponent:@"unzip"]]; |
| 100 |
[pluginTask setCurrentDirectoryPath:curPath]; |
| 101 |
NSString* argument = @"-Z"; |
| 102 |
[pluginTask setArguments:[NSArray arrayWithObjects:argument,@"--incode=sjis",filePath,nil]]; |
| 103 |
|
| 104 |
[pluginTask launch]; |
| 105 |
|
| 106 |
// ��������������������������� |
| 107 |
NSMutableData* data = [NSMutableData data]; |
| 108 |
NSData* outData,*errData; |
| 109 |
while (((outData = [[stdoutPipe fileHandleForReading] availableData]) && [outData length]) || |
| 110 |
((errData = [[stderrPipe fileHandleForReading] availableData]) && [errData length])) { |
| 111 |
if ([outData length]) |
| 112 |
[data appendData:outData]; |
| 113 |
} |
| 114 |
[pluginTask waitUntilExit]; |
| 115 |
int error = [pluginTask terminationStatus]; |
| 116 |
if (error != 0) { |
| 117 |
return NULL; |
| 118 |
} else { |
| 119 |
NSString* string = [[[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding] autorelease]; |
| 120 |
NSMutableArray* array = [NSMutableArray arrayWithArray:[string componentsSeparatedByString:@"\n"]]; |
| 121 |
|
| 122 |
LPFile* root = [[[LPFile alloc] init] autorelease]; |
| 123 |
|
| 124 |
BOOL isDir; |
| 125 |
unsigned int numOfChildren = 0; |
| 126 |
unsigned long long totalSize = 0; |
| 127 |
NSString* parsedString; |
| 128 |
[array removeObjectAtIndex:0]; |
| 129 |
[array removeLastObject]; |
| 130 |
[array removeLastObject]; |
| 131 |
NSEnumerator* enumerator = [array objectEnumerator]; |
| 132 |
|
| 133 |
while(parsedString = [enumerator nextObject]) { |
| 134 |
if ([parsedString length] <= 1) break; |
| 135 |
NSArray* infoPlainArray = [parsedString componentsSeparatedByString:@" "]; |
| 136 |
NSMutableArray* infoArray = [NSMutableArray array]; |
| 137 |
int i,j; |
| 138 |
for (i=0,j=0;i<[infoPlainArray count] && j<8;i++) { |
| 139 |
if ([(NSString*)[infoPlainArray objectAtIndex:i] length] > 0) { |
| 140 |
[infoArray addObject:[infoPlainArray objectAtIndex:i]]; |
| 141 |
j++; |
| 142 |
} |
| 143 |
} |
| 144 |
if (i>=[infoPlainArray count]) break; |
| 145 |
NSMutableString* path = [NSMutableString stringWithString:[infoPlainArray objectAtIndex:i]]; |
| 146 |
for (i++;i<[infoPlainArray count];i++) { |
| 147 |
[path appendString:@" "]; |
| 148 |
if ([(NSString*)[infoPlainArray objectAtIndex:i] length] > 0) |
| 149 |
[path appendString:[infoPlainArray objectAtIndex:i]]; |
| 150 |
} |
| 151 |
isDir = [[infoArray objectAtIndex:0] hasPrefix:@"d"]; |
| 152 |
int fileSize = [[infoArray objectAtIndex:3] intValue]; |
| 153 |
totalSize += fileSize; |
| 154 |
NSNumber* size = [NSNumber numberWithInt:fileSize]; |
| 155 |
NSDate* date = [NSDate dateWithNaturalLanguageString:[NSString stringWithFormat:@"%@ %@",[infoArray objectAtIndex:6],[infoArray objectAtIndex:7]]]; |
| 156 |
[root appendFile:[[LPFile alloc] init:path name:nil modifiedDate:date createdDate:nil size:size kind:nil isDir:isDir] hierarchy:hierarchy]; |
| 157 |
numOfChildren++; |
| 158 |
_totalInArchiveFileCount++; |
| 159 |
} |
| 160 |
|
| 161 |
[root setTotalNumOfChildren:[NSNumber numberWithUnsignedInt:numOfChildren]]; |
| 162 |
[root setTotalSize:[NSNumber numberWithUnsignedLongLong:totalSize]]; |
| 163 |
return root; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
//������path������������ |
| 169 |
- (BOOL)archiveAllUncompress:(NSString*)path exclude:(NSArray*)exclude password:(NSString*)password delegate:(id)delegate selector:(SEL)selector |
| 170 |
{ |
| 171 |
_receiver = delegate; |
| 172 |
_selector = selector; |
| 173 |
|
| 174 |
if (!filePath) { |
| 175 |
objc_msgSend(delegate, selector, nil); |
| 176 |
return NO; |
| 177 |
} |
| 178 |
|
| 179 |
_stdoutArray = [[NSMutableArray array] retain]; |
| 180 |
_stderrArray = [[NSMutableArray array] retain]; |
| 181 |
|
| 182 |
_totalFileCount = _totalInArchiveFileCount; |
| 183 |
if (_totalFileCount == 0) { |
| 184 |
_totalFileCount = [self containFilesCount]; |
| 185 |
NSLog(@"total number of in-files is %d",_totalFileCount); |
| 186 |
} |
| 187 |
|
| 188 |
_pluginTask = [[NSTask alloc] init]; |
| 189 |
// ������������������������������ |
| 190 |
[_pluginTask setStandardOutput:[NSPipe pipe]]; |
| 191 |
_pluginStdoutHandle = [[[_pluginTask standardOutput] fileHandleForReading] retain]; |
| 192 |
[_pluginTask setStandardError:[NSPipe pipe]]; |
| 193 |
_pluginStderrHandle = [[[_pluginTask standardError] fileHandleForReading] retain]; |
| 194 |
|
| 195 |
[[NSNotificationCenter defaultCenter] addObserver: self |
| 196 |
selector: @selector(readPipe:) |
| 197 |
name: NSFileHandleReadCompletionNotification |
| 198 |
object: _pluginStdoutHandle]; |
| 199 |
[[NSNotificationCenter defaultCenter] addObserver: self |
| 200 |
selector: @selector(readPipe:) |
| 201 |
name: NSFileHandleReadCompletionNotification |
| 202 |
object: _pluginStderrHandle]; |
| 203 |
[[NSNotificationCenter defaultCenter] addObserver:self |
| 204 |
selector:@selector(terminateTask:) |
| 205 |
name:NSTaskDidTerminateNotification |
| 206 |
object:_pluginTask]; |
| 207 |
[_pluginStdoutHandle readInBackgroundAndNotify]; |
| 208 |
[_pluginStderrHandle readInBackgroundAndNotify]; |
| 209 |
|
| 210 |
_stdoutEmpty = _stderrEmpty = _terminatedTask = NO; |
| 211 |
|
| 212 |
//������������������������������������ |
| 213 |
NSString* curPath = [NSString stringWithString:path]; |
| 214 |
// ������ |
| 215 |
//[_pluginTask setLaunchPath:@"/usr/bin/unzip"]; |
| 216 |
[_pluginTask setLaunchPath:[[[NSBundle bundleForClass:[self class]] resourcePath] stringByAppendingPathComponent:@"unzip"]]; |
| 217 |
[_pluginTask setCurrentDirectoryPath:curPath]; |
| 218 |
NSMutableArray* extractArray = (password) ? [NSMutableArray arrayWithObjects:@"-P",password,nil] : [NSMutableArray array]; |
| 219 |
[extractArray addObjectsFromArray:[NSArray arrayWithObjects:@"--incode=sjis",filePath,nil]]; //!������������������������������ |
| 220 |
if (exclude) { |
| 221 |
[extractArray addObject:@"-x"]; |
| 222 |
[extractArray addObjectsFromArray:exclude]; |
| 223 |
} |
| 224 |
[_pluginTask setArguments:extractArray]; |
| 225 |
_processMode = 2; //!��������������������������� |
| 226 |
|
| 227 |
[_pluginTask launch]; |
| 228 |
return YES; |
| 229 |
} |
| 230 |
|
| 231 |
//������path������������ |
| 232 |
- (BOOL)archiveUncompress:(NSString*)path files:(NSArray*)files password:(NSString*)password delegate:(id)delegate selector:(SEL)selector |
| 233 |
{ |
| 234 |
_receiver = delegate; |
| 235 |
_selector = selector; |
| 236 |
|
| 237 |
if (!filePath) { |
| 238 |
objc_msgSend(delegate, selector, LPErrorBadArgument); |
| 239 |
return NO; |
| 240 |
} |
| 241 |
|
| 242 |
_stdoutArray = [[NSMutableArray array] retain]; |
| 243 |
_stderrArray = [[NSMutableArray array] retain]; |
| 244 |
|
| 245 |
_pluginTask = [[NSTask alloc] init]; |
| 246 |
// ������������������������������ |
| 247 |
[_pluginTask setStandardOutput:[NSPipe pipe]]; |
| 248 |
_pluginStdoutHandle = [[[_pluginTask standardOutput] fileHandleForReading] retain]; |
| 249 |
[_pluginTask setStandardError:[NSPipe pipe]]; |
| 250 |
_pluginStderrHandle = [[[_pluginTask standardError] fileHandleForReading] retain]; |
| 251 |
|
| 252 |
//������������������������������������������������������������������������ |
| 253 |
NSString* curPath = [NSString stringWithString:path]; |
| 254 |
|
| 255 |
[[NSNotificationCenter defaultCenter] addObserver: self |
| 256 |
selector: @selector(readPipe:) |
| 257 |
name: NSFileHandleReadCompletionNotification |
| 258 |
object: _pluginStdoutHandle]; |
| 259 |
[[NSNotificationCenter defaultCenter] addObserver: self |
| 260 |
selector: @selector(readPipe:) |
| 261 |
name: NSFileHandleReadCompletionNotification |
| 262 |
object: _pluginStderrHandle]; |
| 263 |
[[NSNotificationCenter defaultCenter] addObserver:self |
| 264 |
selector:@selector(terminateTask:) |
| 265 |
name:NSTaskDidTerminateNotification |
| 266 |
object:_pluginTask]; |
| 267 |
[_pluginStdoutHandle readInBackgroundAndNotify]; |
| 268 |
[_pluginStderrHandle readInBackgroundAndNotify]; |
| 269 |
|
| 270 |
_stdoutEmpty = _stderrEmpty = _terminatedTask = NO; |
| 271 |
|
| 272 |
// ������ |
| 273 |
//[_pluginTask setLaunchPath:@"/usr/bin/unzip"]; |
| 274 |
[_pluginTask setLaunchPath:[[[NSBundle bundleForClass:[self class]] resourcePath] stringByAppendingPathComponent:@"unzip"]]; |
| 275 |
// ������������������������������������������ |
| 276 |
[_pluginTask setCurrentDirectoryPath:curPath]; |
| 277 |
NSMutableArray* extractArray = (password) ? [NSMutableArray arrayWithObjects:@"-P",password,nil] : [NSMutableArray array]; |
| 278 |
[extractArray addObjectsFromArray:[NSMutableArray arrayWithObjects:@"--incode=sjis",filePath,nil]]; //!������������������������������ |
| 279 |
|
| 280 |
LPFile* file; |
| 281 |
NSEnumerator* enumerator = [files objectEnumerator]; |
| 282 |
_totalFileCount = 0; |
| 283 |
// ��������������������������������������������������������������������������������� |
| 284 |
while (file = [enumerator nextObject]) { |
| 285 |
NSString* path; |
| 286 |
if ([file isDir]) { |
| 287 |
path = [[file path] stringByAppendingPathComponent:@"*"]; |
| 288 |
_totalFileCount += [[file totalNumOfChildren] intValue]; |
| 289 |
} else { |
| 290 |
path = [file path]; |
| 291 |
_totalFileCount++; |
| 292 |
} |
| 293 |
[extractArray addObject:path]; |
| 294 |
} |
| 295 |
|
| 296 |
[_pluginTask setArguments:extractArray]; //!������������������������������ |
| 297 |
_processMode = 2; //!��������������������������� |
| 298 |
|
| 299 |
[_pluginTask launch]; |
| 300 |
return YES; |
| 301 |
} |
| 302 |
|
| 303 |
- (NSMutableData*)archiveUncompressToMemory:(LPFile*)file password:(NSString*)password |
| 304 |
{ |
| 305 |
NSTask* pluginTask = [[[NSTask alloc] init] autorelease]; |
| 306 |
NSPipe* pluginPipe = [NSPipe pipe]; |
| 307 |
//��������������������������������������������������������������������������������������������� |
| 308 |
NSString* curPath = [filePath stringByDeletingLastPathComponent]; |
| 309 |
|
| 310 |
// ������������������������������ |
| 311 |
[pluginTask setStandardOutput:pluginPipe]; |
| 312 |
//[pluginTask setStandardError:pluginPipe]; |
| 313 |
|
| 314 |
// ������ |
| 315 |
[pluginTask setLaunchPath:[[[NSBundle bundleForClass:[self class]] resourcePath] stringByAppendingPathComponent:@"unzip"]]; |
| 316 |
[pluginTask setCurrentDirectoryPath:curPath]; |
| 317 |
NSMutableArray* extractArray = (password) ? [NSMutableArray arrayWithObjects:@"-p",@"-P",password,nil] : [NSMutableArray arrayWithObject:@"-p"]; |
| 318 |
[extractArray addObjectsFromArray:[NSMutableArray arrayWithObjects:@"--incode=sjis",filePath,[file path],nil]]; |
| 319 |
[pluginTask setArguments:extractArray]; |
| 320 |
[pluginTask launch]; |
| 321 |
|
| 322 |
NSMutableData* data = [NSMutableData data]; |
| 323 |
NSData* inData; |
| 324 |
while ((inData = [[pluginPipe fileHandleForReading] availableData]) && [inData length]) { |
| 325 |
[data appendData:inData]; |
| 326 |
} |
| 327 |
return data; |
| 328 |
} |
| 329 |
|
| 330 |
- (BOOL)archiveCompress:(NSString*)path files:(NSArray*)files exclude:(NSArray*)exclude currentPath:(NSString*)currentPath compress:(LPCompress*)compress option:(NSDictionary*)option delegate:(id)delegate selector:(SEL)selector; |
| 331 |
{ |
| 332 |
_receiver = delegate; |
| 333 |
_selector = selector; |
| 334 |
|
| 335 |
if (!filePath) { |
| 336 |
objc_msgSend(delegate, selector, LPErrorBadArgument); |
| 337 |
return NO; |
| 338 |
} |
| 339 |
|
| 340 |
_stdoutArray = [[NSMutableArray array] retain]; |
| 341 |
_stderrArray = [[NSMutableArray array] retain]; |
| 342 |
//_totalFileCount = [files count]; |
| 343 |
_totalFileCount = 0; |
| 344 |
NSFileManager* manager = [NSFileManager defaultManager]; |
| 345 |
NSEnumerator* enumerator = [files objectEnumerator]; |
| 346 |
NSString* directoryPath; |
| 347 |
while (directoryPath = [enumerator nextObject]) { |
| 348 |
_totalFileCount += [[manager subpathsAtPath:[NSString stringWithFormat:@"%@/%@",currentPath,directoryPath]] count]; |
| 349 |
} |
| 350 |
|
| 351 |
_pluginTask = [[NSTask alloc] init]; |
| 352 |
// ������������������������������ |
| 353 |
[_pluginTask setStandardOutput:[NSPipe pipe]]; |
| 354 |
_pluginStdoutHandle = [[[_pluginTask standardOutput] fileHandleForReading] retain]; |
| 355 |
[_pluginTask setStandardError:[NSPipe pipe]]; |
| 356 |
_pluginStderrHandle = [[[_pluginTask standardError] fileHandleForReading] retain]; |
| 357 |
//��������������������������������������������������������������������������������������� |
| 358 |
NSString* curPath = [NSString stringWithString:currentPath]; |
| 359 |
|
| 360 |
[[NSNotificationCenter defaultCenter] addObserver: self |
| 361 |
selector: @selector(readPipe:) |
| 362 |
name: NSFileHandleReadCompletionNotification |
| 363 |
object: _pluginStdoutHandle]; |
| 364 |
[[NSNotificationCenter defaultCenter] addObserver: self |
| 365 |
selector: @selector(readPipe:) |
| 366 |
name: NSFileHandleReadCompletionNotification |
| 367 |
object: _pluginStderrHandle]; |
| 368 |
[[NSNotificationCenter defaultCenter] addObserver:self |
| 369 |
selector:@selector(terminateTask:) |
| 370 |
name:NSTaskDidTerminateNotification |
| 371 |
object:_pluginTask]; |
| 372 |
[_pluginStdoutHandle readInBackgroundAndNotify]; |
| 373 |
[_pluginStderrHandle readInBackgroundAndNotify]; |
| 374 |
|
| 375 |
_stdoutEmpty = _stderrEmpty = _terminatedTask = NO; |
| 376 |
|
| 377 |
// ������ |
| 378 |
[_pluginTask setLaunchPath:[[[NSBundle bundleForClass:[self class]] resourcePath] stringByAppendingPathComponent:@"zip"]]; |
| 379 |
// ������������������������������������������ |
| 380 |
[_pluginTask setCurrentDirectoryPath:curPath]; |
| 381 |
NSString* argument = @"-r"; |
| 382 |
NSMutableArray* compressArray = [NSMutableArray arrayWithObjects:argument, filePath, nil]; |
| 383 |
[compressArray addObjectsFromArray:files]; |
| 384 |
if (exclude) { |
| 385 |
[compressArray addObject:@"-x"]; |
| 386 |
[compressArray addObjectsFromArray:exclude]; |
| 387 |
} |
| 388 |
[_pluginTask setArguments:compressArray]; //!������������������������������ |
| 389 |
_processMode = 3; //!��������������������������� |
| 390 |
|
| 391 |
[_pluginTask launch]; |
| 392 |
return YES; |
| 393 |
} |
| 394 |
|
| 395 |
//! ��������������������������������������������������������������� |
| 396 |
- (int)containFilesCount |
| 397 |
{ |
| 398 |
NSTask* pluginTask = [[[NSTask alloc] init] autorelease]; |
| 399 |
NSPipe* pluginPipe = [NSPipe pipe]; |
| 400 |
//��������������������������������������������������������������������������������������������� |
| 401 |
NSString* curPath = [filePath stringByDeletingLastPathComponent]; |
| 402 |
|
| 403 |
// ������������������������������ |
| 404 |
[pluginTask setStandardOutput:pluginPipe]; |
| 405 |
//[pluginTask setStandardError:pluginPipe]; |
| 406 |
|
| 407 |
// ������ |
| 408 |
[pluginTask setLaunchPath:[[[NSBundle bundleForClass:[self class]] resourcePath] stringByAppendingPathComponent:@"unzip"]]; |
| 409 |
[pluginTask setCurrentDirectoryPath:curPath]; |
| 410 |
NSString* argument = @"-l"; |
| 411 |
[pluginTask setArguments:[NSArray arrayWithObjects:argument,@"--incode=sjis",filePath,nil]]; |
| 412 |
|
| 413 |
[pluginTask launch]; |
| 414 |
|
| 415 |
NSMutableData* data = [NSMutableData data]; |
| 416 |
NSData* inData; |
| 417 |
while ((inData = [[pluginPipe fileHandleForReading] availableData]) && [inData length]) { |
| 418 |
[data appendData:inData]; |
| 419 |
} |
| 420 |
NSString* string = [[[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding] autorelease]; |
| 421 |
NSArray* array = [string componentsSeparatedByString:@"\n"]; |
| 422 |
return [array count]-6; |
| 423 |
} |
| 424 |
|
| 425 |
//������������������������������������������������������������ |
| 426 |
- (void)readPipe:(NSNotification*)notification |
| 427 |
{ |
| 428 |
//NSLog(@"readPipe"); |
| 429 |
NSData *data = [[notification userInfo] objectForKey: NSFileHandleNotificationDataItem]; |
| 430 |
if ([data length]) { |
| 431 |
NSString* string = [[[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding] autorelease]; |
| 432 |
NSMutableArray* array = [NSMutableArray arrayWithArray:[string componentsSeparatedByString:@"\n"]]; |
| 433 |
NSMutableArray* printoutArray = [[notification object] isEqualTo:_pluginStdoutHandle] ? _stdoutArray : _stderrArray; |
| 434 |
if ([printoutArray count] > 0) { |
| 435 |
if ([(NSString*)[printoutArray lastObject] length] != 0) { //! ������������������������������������������������ |
| 436 |
[array replaceObjectAtIndex:0 withObject:[[printoutArray lastObject] stringByAppendingString:[array objectAtIndex:0]]]; |
| 437 |
} |
| 438 |
[printoutArray removeLastObject]; |
| 439 |
} |
| 440 |
[printoutArray addObjectsFromArray:array]; |
| 441 |
if ([[notification object] isEqualTo:_pluginStdoutHandle]) { |
| 442 |
_stdoutEmpty = NO; |
| 443 |
} else if ([[notification object] isEqualTo:_pluginStderrHandle]) { |
| 444 |
_stderrEmpty = NO; |
| 445 |
} |
| 446 |
//��������������������������������������������������������������������������������������������������������������������������������� |
| 447 |
[[notification object] readInBackgroundAndNotify]; |
| 448 |
} else { |
| 449 |
NSLog(@"Data length=0!"); |
| 450 |
if ([[notification object] isEqualTo:_pluginStdoutHandle]) { |
| 451 |
_stdoutEmpty = YES; |
| 452 |
} else if ([[notification object] isEqualTo:_pluginStderrHandle]) { |
| 453 |
_stderrEmpty = YES; |
| 454 |
} |
| 455 |
if (_stdoutEmpty && _stderrEmpty && _terminatedTask) |
| 456 |
[self stopTask]; |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
- (void)terminateTask:(NSNotification*)notification |
| 461 |
{ |
| 462 |
NSLog(@"terminateTask"); |
| 463 |
_terminatedTask = YES; |
| 464 |
if(_stdoutEmpty && _stderrEmpty){ |
| 465 |
[self stopTask]; |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
- (void)stopTask |
| 470 |
{ |
| 471 |
NSLog(@"stopTask"); |
| 472 |
int error = [_pluginTask terminationStatus]; |
| 473 |
[[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 474 |
|
| 475 |
[_pluginTask release]; |
| 476 |
[_pluginStdoutHandle release]; |
| 477 |
[_pluginStderrHandle release]; |
| 478 |
|
| 479 |
/*if (_processMode == 1) { //! ��������������������� |
| 480 |
} else */if (_processMode == 2) { |
| 481 |
[_stdoutArray release]; |
| 482 |
[_stderrArray release]; |
| 483 |
NSLog(@"ZipPlugin Error Code=%d",error); |
| 484 |
switch(error) { |
| 485 |
case 0: |
| 486 |
objc_msgSend(_receiver, _selector, LPErrorNoError); |
| 487 |
break; |
| 488 |
case 1: |
| 489 |
case 2: |
| 490 |
objc_msgSend(_receiver, _selector, LPErrorWarning); |
| 491 |
break; |
| 492 |
case 3: |
| 493 |
objc_msgSend(_receiver, _selector, LPErrorBrokenArchive); |
| 494 |
break; |
| 495 |
case 4: |
| 496 |
case 6: |
| 497 |
case 7: |
| 498 |
objc_msgSend(_receiver, _selector, LPErrorFailureAllocateMemory); |
| 499 |
break; |
| 500 |
case 5: |
| 501 |
objc_msgSend(_receiver, _selector, LPErrorRequestPassword); |
| 502 |
break; |
| 503 |
case 9: |
| 504 |
objc_msgSend(_receiver, _selector, LPErrorNotExist); |
| 505 |
break; |
| 506 |
case 10: |
| 507 |
objc_msgSend(_receiver, _selector, LPErrorNotImplementMethod); |
| 508 |
break; |
| 509 |
case 50: |
| 510 |
objc_msgSend(_receiver, _selector, LPErrorFileWriting); |
| 511 |
break; |
| 512 |
case 51: |
| 513 |
objc_msgSend(_receiver, _selector, LPErrorBrokenArchive); |
| 514 |
break; |
| 515 |
case 81: |
| 516 |
objc_msgSend(_receiver, _selector, LPErrorNotSupportFormat); |
| 517 |
break; |
| 518 |
case 82: |
| 519 |
objc_msgSend(_receiver, _selector, LPErrorBadPassword); |
| 520 |
break; |
| 521 |
default: |
| 522 |
objc_msgSend(_receiver, _selector, LPErrorUnknown); |
| 523 |
} |
| 524 |
} else if (_processMode == 3) { |
| 525 |
[_stdoutArray release]; |
| 526 |
[_stderrArray release]; |
| 527 |
NSLog(@"ZipPlugin Error Code=%d",error); |
| 528 |
switch(error) { |
| 529 |
case 0: |
| 530 |
objc_msgSend(_receiver, _selector, LPErrorNoError); |
| 531 |
break; |
| 532 |
case 4: |
| 533 |
case 8: |
| 534 |
objc_msgSend(_receiver, _selector, LPErrorFailureAllocateMemory); |
| 535 |
break; |
| 536 |
case 3: |
| 537 |
case 5: |
| 538 |
objc_msgSend(_receiver, _selector, LPErrorBrokenArchive); |
| 539 |
break; |
| 540 |
case 11: |
| 541 |
objc_msgSend(_receiver, _selector, LPErrorFileReading); |
| 542 |
break; |
| 543 |
case 7: |
| 544 |
case 16: |
| 545 |
objc_msgSend(_receiver, _selector, LPErrorBadArgument); |
| 546 |
break; |
| 547 |
case 14: |
| 548 |
case 15: |
| 549 |
objc_msgSend(_receiver, _selector, LPErrorFileWriting); |
| 550 |
break; |
| 551 |
case 18: |
| 552 |
objc_msgSend(_receiver, _selector, LPErrorNotExist); |
| 553 |
default: |
| 554 |
objc_msgSend(_receiver, _selector, LPErrorUnknown); |
| 555 |
} |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
- (BOOL)pauseProcess |
| 560 |
{ |
| 561 |
return NO; |
| 562 |
} |
| 563 |
|
| 564 |
- (BOOL)continueProcess |
| 565 |
{ |
| 566 |
return NO; |
| 567 |
} |
| 568 |
|
| 569 |
- (BOOL)stopProcess |
| 570 |
{ |
| 571 |
return NO; |
| 572 |
} |
| 573 |
|
| 574 |
#pragma mark Process Information |
| 575 |
/*! |
| 576 |
return whether plugin can get progress of process. |
| 577 |
*/ |
| 578 |
- (BOOL)isProgressTraversable |
| 579 |
{ |
| 580 |
return YES; |
| 581 |
} |
| 582 |
|
| 583 |
- (NSNumber*)processInfo:(NSString**)title |
| 584 |
{ |
| 585 |
//title��������������������������������������������������������������������������������������������������������������� |
| 586 |
if (!_pluginTask) return nil; |
| 587 |
if ([_pluginTask isRunning] && _stdoutArray) { |
| 588 |
if ([_stdoutArray count] > 1) { |
| 589 |
NSString* string = [NSString stringWithString:[_stdoutArray objectAtIndex:[_stdoutArray count]-2]]; |
| 590 |
*title = (_processMode == 3 && [string length] > 10) ? [string substringFromIndex:10] : string; |
| 591 |
return [NSNumber numberWithFloat:100*([_stdoutArray count]-1)/_totalFileCount]; |
| 592 |
} else { |
| 593 |
return nil; |
| 594 |
} |
| 595 |
} else { |
| 596 |
return [NSNumber numberWithInt:100]; |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
#pragma mark Compress Information |
| 601 |
- (NSArray*)compressInfo:(int)index |
| 602 |
{ |
| 603 |
LPCompress* zipCompress = [[[LPCompress alloc] init:@"ZIP Compress" extension:@"zip" hasOptionView:YES index:index] autorelease]; |
| 604 |
return [NSArray arrayWithObjects:zipCompress,nil]; |
| 605 |
} |
| 606 |
|
| 607 |
@end |