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
|
#import <Foundation/Foundation.h>
#import "LzxArchive.h"
#import "FileInfo.h"
#import "NSString+Custom.h"
#import "Preferences.h"
#import "NSArray+Custom.h"
@interface LzxArchive (PrivateAPI)
- (NSData *)dataByRunningLzx;
@end
@implementation LzxArchive : Archive
/**
* register our supported file extensions with our superclass.
*/
+ (void)initialize
{
[self registerFileExtension:@"lzx" forArchiveClass:self];
}
+ (NSString *)unarchiveExecutable
{
return [Preferences lzxExecutable];
}
/**
* lzx archives can only be unpacked with path info
*/
+ (BOOL)canExtractWithoutFullPath
{
return NO;
}
/**
* lzx archives <em>do not</em> contain info about compression ratio.
*/
+ (BOOL)hasRatio;
{
return NO;
}
+ (ArchiveType)archiveType
{
return LZX;
}
//------------------------------------------------------------------------------
// expanding the archive
//------------------------------------------------------------------------------
/**
* the unlzx command does not allow to unpack single files from the archive. We
* resort to unpacking the entire archive instead ...
*/
- (int)expandFiles:(NSArray *)files withPathInfo:(BOOL)usePathInfo toPath:(NSString *)path
{
NSArray *args;
args = [NSArray arrayWithObjects:@"-x", @"--", [self path], nil];
return [[self class] runUnarchiverWithArguments:args inDirectory:path];
}
- (NSArray *)listContents
{
NSEnumerator *cursor;
NSString *line;
NSMutableArray *results = [NSMutableArray array];
NSData *data = [self dataByRunningLzx];
NSString *string = [[[NSString alloc] initWithData:data
encoding:NSASCIIStringEncoding] autorelease];
NSArray *lines = [string componentsSeparatedByString:@"\n"];
// take out first 2 lines (header) and last 2 lines (footer)
lines = [lines subarrayWithRange:NSMakeRange(2, [lines count] - 2)];
lines = [lines subarrayWithRange:NSMakeRange(0, [lines count] - 3)];
cursor = [lines objectEnumerator];
while ((line = [cursor nextObject]) != nil)
{
NSArray *components;
int length;
NSString *path, *dateString, *timeString;
NSCalendarDate *date;
FileInfo *info;
components = [line componentsSeparatedByString:@" "];
components = [components arrayByRemovingEmptyStrings];
timeString = [components objectAtIndex:2];
if ([timeString isEqual:@"Merged"])
{
// skip lines that continue "Merged" in the time column, they contain no usable info
continue;
}
length = [[components objectAtIndex:0] intValue];
path = [components objectAtIndex:5];
if ([path hasPrefix:@"\""])
{
path = [path substringFromIndex:1];
}
if ([path hasSuffix:@"\""])
{
path = [path substringToIndex:[path length] - 1];
}
dateString = [components objectAtIndex:3];
dateString = [NSString stringWithFormat:@"%@ %@", dateString, timeString];
date = [NSCalendarDate dateWithString:dateString calendarFormat:@"%d-%b-%Y %H:%M:%S"];
info = [FileInfo newWithPath:path date:date size:[NSNumber numberWithInt:length]];
[results addObject:info];
}
return results;
}
//------------------------------------------------------------------------------
// private API
//------------------------------------------------------------------------------
- (NSData *)dataByRunningLzx
{
NSData *data;
NSArray *args = [NSArray arrayWithObjects:@"-v", @"--", [self path], nil];
data = [self dataByRunningUnachiverWithArguments:args];
return data;
}
@end
|