~ubuntu-branches/ubuntu/trusty/zipper.app/trusty

« back to all changes in this revision

Viewing changes to LhaArchive.m

  • Committer: Bazaar Package Importer
  • Author(s): Gürkan Sengün
  • Date: 2004-11-10 23:40:33 UTC
  • Revision ID: james.westby@ubuntu.com-20041110234033-v32mldso2yf8i7v4
Tags: upstream-0.9
Import upstream version 0.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#import <Foundation/Foundation.h>
 
2
#import "LhaArchive.h"
 
3
#import "FileInfo.h"
 
4
#import "NSString+Custom.h"
 
5
#import "Preferences.h"
 
6
#import "NSArray+Custom.h"
 
7
 
 
8
@interface LhaArchive (PrivateAPI)
 
9
- (NSData *)dataByRunningLha;
 
10
@end
 
11
 
 
12
@implementation LhaArchive : Archive
 
13
 
 
14
/**
 
15
 * register our supported file extensions with our superclass.
 
16
 */
 
17
+ (void)initialize
 
18
{
 
19
        [self registerFileExtension:@"lha" forArchiveClass:self];
 
20
}
 
21
 
 
22
+ (NSString *)unarchiveExecutable
 
23
{
 
24
        return [Preferences lhaExecutable];
 
25
}
 
26
 
 
27
/**
 
28
 * lha archives <em>do</em> contain info about compression ratio.
 
29
 */
 
30
+ (BOOL)hasRatio;
 
31
{
 
32
        return YES;
 
33
}
 
34
 
 
35
+ (NSString *)archiveType
 
36
{
 
37
        return @"LHA";  
 
38
}
 
39
 
 
40
//------------------------------------------------------------------------------
 
41
// expanding the archive
 
42
//------------------------------------------------------------------------------
 
43
- (int)expandFiles:(NSArray *)files withPathInfo:(BOOL)usePathInfo toPath:(NSString *)path
 
44
{
 
45
        FileInfo *fileInfo;
 
46
        NSMutableArray *args;
 
47
        NSString *argString;
 
48
        
 
49
        argString = @"e";
 
50
        if (usePathInfo == NO)
 
51
        {
 
52
                argString = [argString stringByAppendingString:@"i"];
 
53
        }
 
54
        // destination dir
 
55
        argString = [argString stringByAppendingString:@"w="];
 
56
        argString = [argString stringByAppendingString:path];
 
57
 
 
58
        args = [NSMutableArray array];
 
59
        [args addObject:argString];
 
60
        [args addObject:[self path]];
 
61
        
 
62
        if (files != nil)
 
63
        {
 
64
                NSEnumerator *cursor = [files objectEnumerator];
 
65
                while ((fileInfo = [cursor nextObject]) != nil)
 
66
                {
 
67
                        [args addObject:[fileInfo fullPath]];
 
68
                }
 
69
        }
 
70
        
 
71
        return [self runUnarchiverWithArguments:args];
 
72
}
 
73
 
 
74
- (NSArray *)listContents
 
75
{
 
76
        NSEnumerator *cursor;
 
77
        NSString *line;
 
78
    
 
79
    NSMutableArray *results = [NSMutableArray array];
 
80
    NSData *data = [self dataByRunningLha];
 
81
    NSString *string = [[[NSString alloc] initWithData:data 
 
82
                encoding:NSASCIIStringEncoding] autorelease];
 
83
    NSArray *lines = [string componentsSeparatedByString:@"\n"];
 
84
 
 
85
    // take out first 2 lines (header) and last 2 lines (footer)
 
86
    lines = [lines subarrayWithRange:NSMakeRange(2, [lines count] - 2)];
 
87
    lines = [lines subarrayWithRange:NSMakeRange(0, [lines count] - 3)];
 
88
 
 
89
        cursor = [lines objectEnumerator];
 
90
        while ((line = [cursor nextObject]) != nil)
 
91
        {
 
92
                NSArray *components;
 
93
                int length;
 
94
                NSString *path, *ratio, *month, *day, *year, *dateString;
 
95
                NSCalendarDate *date;
 
96
                FileInfo *info;
 
97
                
 
98
                if ([line hasSuffix:@"/"])
 
99
                {
 
100
                        // skip directory entries
 
101
                        continue;
 
102
                }
 
103
                
 
104
                components = [line componentsSeparatedByString:@" "];
 
105
                components = [components arrayByRemovingEmptyStrings];
 
106
                
 
107
                length = [[components objectAtIndex:3] intValue];
 
108
                path = [components objectAtIndex:10];
 
109
                ratio = [components objectAtIndex:4];
 
110
                
 
111
                month = [components objectAtIndex:7];
 
112
                day = [components objectAtIndex:8];
 
113
                year = [components objectAtIndex:9];
 
114
                dateString = [NSString stringWithFormat:@"%@ %@ %@", month, day, year];
 
115
                date = [NSCalendarDate dateWithString:dateString calendarFormat:@"%b %d %Y"];
 
116
                
 
117
                info = [FileInfo newWithPath:path date:date size:[NSNumber numberWithInt:length]
 
118
                        ratio:ratio];
 
119
                [results addObject:info];
 
120
        }        
 
121
 
 
122
    return results;
 
123
}
 
124
 
 
125
//------------------------------------------------------------------------------
 
126
// private API
 
127
//------------------------------------------------------------------------------
 
128
- (NSData *)dataByRunningLha
 
129
{
 
130
        NSData *data;
 
131
        NSArray *args = [NSArray arrayWithObjects:@"v", [self path], nil];
 
132
        data = [self dataByRunningUnachiverWithArguments:args];
 
133
        NSLog(@"dataByRunningLha: %@", data);
 
134
        return data;
 
135
}
 
136
 
 
137
@end