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

« back to all changes in this revision

Viewing changes to TarArchive.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 "TarArchive.h"
 
3
#import "FileInfo.h"
 
4
#import "NSString+Custom.h"
 
5
#import "Preferences.h"
 
6
#import "NSArray+Custom.h"
 
7
 
 
8
@interface TarArchive (PrivateAPI)
 
9
- (NSData *)dataByRunningTar;
 
10
- (FileInfo *)fileInfoFromLine:(NSString *)line;
 
11
@end
 
12
 
 
13
@implementation TarArchive : Archive
 
14
 
 
15
/**
 
16
 * register our supported file extensions with superclass.
 
17
 */
 
18
+ (void)initialize
 
19
{
 
20
        [self registerFileExtension:@"tar" forArchiveClass:self];
 
21
        [self registerFileExtension:@"gz" forArchiveClass:self];
 
22
        [self registerFileExtension:@"tgz" forArchiveClass:self];
 
23
        [self registerFileExtension:@"bz2" forArchiveClass:self];
 
24
}
 
25
 
 
26
+ (NSString *)unarchiveExecutable
 
27
{
 
28
        return [Preferences tarExecutable];
 
29
}
 
30
 
 
31
/**
 
32
 * Tar files inherently have the full path info and can't be uncompressed flat.
 
33
 */
 
34
+ (BOOL)canExtractWithoutFullPath;
 
35
{
 
36
        return NO;
 
37
}
 
38
 
 
39
+ (NSString *)archiveType
 
40
{
 
41
        return @"tar";
 
42
}
 
43
 
 
44
//------------------------------------------------------------------------------
 
45
// expanding the archive
 
46
//------------------------------------------------------------------------------
 
47
- (int)expandFiles:(NSArray *)files withPathInfo:(BOOL)usePathInfo toPath:(NSString *)path
 
48
{
 
49
        FileInfo *fileInfo;
 
50
        NSString *compressionArg;
 
51
        NSMutableArray *args;
 
52
        
 
53
        compressionArg = [Preferences compressionArgumentForFile:[self path]];
 
54
        NSParameterAssert(compressionArg != nil);
 
55
 
 
56
        args = [NSMutableArray array];
 
57
        [args addObject:@"-x"];
 
58
        // compression method
 
59
        [args addObject:compressionArg];
 
60
        // destination dir
 
61
        [args addObject:@"-C"];
 
62
        [args addObject:path];
 
63
        // the archive
 
64
        [args addObject:@"-f"];
 
65
        [args addObject:[self path]];
 
66
        
 
67
        if (files != nil)
 
68
        {
 
69
                NSEnumerator *cursor = [files objectEnumerator];
 
70
                while ((fileInfo = [cursor nextObject]) != nil)
 
71
                {
 
72
                        [args addObject:[fileInfo fullPath]];
 
73
                }
 
74
        }
 
75
        
 
76
        return [self runUnarchiverWithArguments:args];
 
77
}
 
78
 
 
79
- (NSArray *)listContents
 
80
{
 
81
    NSString *line;
 
82
 
 
83
    NSMutableArray *results = [NSMutableArray array];
 
84
    NSData *data = [self dataByRunningTar];
 
85
    NSString *string = [[[NSString alloc] initWithData:data 
 
86
                encoding:NSASCIIStringEncoding] autorelease];
 
87
    NSArray *lines = [string componentsSeparatedByString:@"\n"];
 
88
    
 
89
    NSEnumerator *cursor = [lines objectEnumerator];
 
90
    while ((line = [cursor nextObject]) != nil)
 
91
    {
 
92
                FileInfo *info;
 
93
                
 
94
                // BSD tar seems to add linefeed at the end of the line. strip that
 
95
                if ([line hasSuffix:@"\r"])
 
96
                {
 
97
                        line = [line substringToIndex:[line length] - 1];
 
98
                }
 
99
 
 
100
                // we skip empty lines and plain directory entries
 
101
                if (([line length] == 0) || [line hasSuffix:@"/"])
 
102
                {
 
103
                        continue;
 
104
                }
 
105
                
 
106
                info = [self fileInfoFromLine:line];
 
107
                [results addObject:info];
 
108
        }
 
109
        return results;
 
110
}
 
111
 
 
112
//------------------------------------------------------------------------------
 
113
// creating archives
 
114
//------------------------------------------------------------------------------
 
115
+ (void)createArchive:(NSString *)archivePath withFiles:(NSArray *)filenames
 
116
{
 
117
        NSEnumerator *filenameCursor;
 
118
        NSString *filename;
 
119
        NSString *workdir;
 
120
        NSMutableArray *arguments;
 
121
        
 
122
        // make sure archivePath has the correct suffix
 
123
        if ([archivePath hasSuffix:@".tar.gz"] == NO)
 
124
        {
 
125
                archivePath = [archivePath stringByAppendingString:@".tar.gz"];
 
126
        }
 
127
        
 
128
        // build arguments for commandline: tar -czf filename <list of files>
 
129
        arguments = [NSMutableArray array];
 
130
        [arguments addObject:@"-czf"];
 
131
        [arguments addObject:archivePath];
 
132
                
 
133
        // filenames contains absolute paths, convert them to relative paths. This works
 
134
        // because you can select only files/directories below a current directory in
 
135
        // GWorkspace so all the files *have* to have a common filesystem root.
 
136
        filenameCursor = [filenames objectEnumerator];
 
137
        while ((filename = [filenameCursor nextObject]) != nil)
 
138
        {
 
139
                [arguments addObject:[filename lastPathComponent]];
 
140
        }
 
141
        
 
142
        // change into this directory when running the task
 
143
        workdir = [[filenames objectAtIndex:0] stringByDeletingLastPathComponent];
 
144
 
 
145
        // create the archive. In the case of TarArchive the unarchiver can also create
 
146
        // archives
 
147
        [self runUnarchiverWithArguments:arguments inDirectory:workdir];
 
148
}
 
149
 
 
150
//------------------------------------------------------------------------------
 
151
// private API
 
152
//------------------------------------------------------------------------------
 
153
- (NSData *)dataByRunningTar
 
154
{
 
155
        NSString *compressionArg;
 
156
        NSMutableArray *arguments;
 
157
        
 
158
        compressionArg = [Preferences compressionArgumentForFile:[self path]];
 
159
        NSParameterAssert(compressionArg != nil);
 
160
        
 
161
        arguments = [NSMutableArray arrayWithObject:@"-tv"]; 
 
162
        if ([compressionArg isEqual:@""] == NO)
 
163
        {
 
164
                [arguments addObject:compressionArg];
 
165
        }
 
166
        [arguments addObject:@"-f"];
 
167
        [arguments addObject:[self path]];
 
168
        
 
169
        return [self dataByRunningUnachiverWithArguments:arguments];
 
170
}
 
171
 
 
172
- (FileInfo *)fileInfoFromLine:(NSString *)line
 
173
{
 
174
        int index, length = -1;
 
175
    NSString *path = nil;
 
176
        NSString *dateString = nil;
 
177
        NSString *time = nil;
 
178
        NSCalendarDate *calendarDate = nil;
 
179
 
 
180
        NSArray *components = [line componentsSeparatedByString:@" "];
 
181
        components = [components arrayByRemovingEmptyStrings];
 
182
 
 
183
        if ([Preferences isBsdTar])
 
184
        {
 
185
                NSArray *dateComponents;
 
186
 
 
187
                // BSD tar
 
188
                length = [[components objectAtIndex:2] intValue];
 
189
 
 
190
                dateComponents = [components subarrayWithRange:NSMakeRange(3, 4)];
 
191
                dateString = [dateComponents componentsJoinedByString:@" "];
 
192
                calendarDate = [NSCalendarDate dateWithString:dateString
 
193
                        calendarFormat:@"%b %d %H:%M %Y"];
 
194
        }
 
195
        else    
 
196
        {
 
197
                // linux tar
 
198
                NSString *date;
 
199
                
 
200
                length = [[components objectAtIndex:2] intValue];
 
201
 
 
202
                date = [components objectAtIndex:3];
 
203
                time = [components objectAtIndex:4];
 
204
            dateString = [NSString stringWithFormat:@"%@ %@", date, time];
 
205
        calendarDate = [NSCalendarDate dateWithString:date 
 
206
                        calendarFormat:@"%Y-%m-%d %H:%M:%S"];   
 
207
        }
 
208
        
 
209
        // The path is everything afer the date string. Since it can contain blanks,
 
210
        // do *not* just grab any objects from components array
 
211
        index = [line rangeOfString:dateString].location;
 
212
        index += [dateString length];
 
213
        path = [[line substringFromIndex:index] stringByRemovingWhitespaceFromBeginning];
 
214
 
 
215
        return [FileInfo newWithPath:path date:calendarDate 
 
216
                size:[NSNumber numberWithInt:length]];
 
217
}
 
218
 
 
219
@end