File: SevenZipArchive.m

package info (click to toggle)
zipper.app 1.5-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 644 kB
  • sloc: objc: 3,829; makefile: 11
file content (193 lines) | stat: -rw-r--r-- 5,631 bytes parent folder | download | duplicates (3)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#import <Foundation/Foundation.h>
#import "SevenZipArchive.h"
#import "FileInfo.h"
#import "NSString+Custom.h"
#import "Preferences.h"
#import "NSArray+Custom.h"

@interface SevenZipArchive (PrivateAPI)
- (NSData *)dataByRunningSevenZip;
- (NSArray *)listSevenZipContents:(NSArray *)lines;
@end

@implementation SevenZipArchive : Archive

/**
 * register our supported file extensions with superclass.
 */
+ (void)initialize
{
	[self registerFileExtension:@"7z" forArchiveClass:self];
	[self registerFileExtension:@"7za" forArchiveClass:self];
}

+ (NSString *)archiveExecutable
{
	return [Preferences sevenZipExecutable];
}
+ (NSString *)unarchiveExecutable
{
	return [Preferences sevenZipExecutable];
}

+ (BOOL)hasRatio;
{
	return NO;
}

+ (ArchiveType)archiveType
{
	return SEVENZIP;
}

//------------------------------------------------------------------------------
// expanding the archive
//------------------------------------------------------------------------------
- (int)expandFiles:(NSArray *)files withPathInfo:(BOOL)usePathInfo toPath:(NSString *)path
{
	FileInfo *fileInfo;
	NSMutableArray *args;
		
	args = [NSMutableArray array];

	if (usePathInfo == YES)
	{
		[args addObject:@"x"];
	}
	else
	{
		// extract flat
		[args addObject:@"e"];
	}

	// assume yes on all queries (needed for silently overwriting files)
	[args addObject:@"-y"];

	// destination dir, path must not be separated with blank from the 'o' option
	[args addObject:[@"-o" stringByAppendingString:path]];

	// protect for archives and files starting with -
	[args addObject:@"--"];	

	// add archive filename	
	[args addObject:[self path]];	

	if (files != nil)
	{
		NSEnumerator *cursor = [files objectEnumerator];
		while ((fileInfo = [cursor nextObject]) != nil)
		{
			[args addObject:[fileInfo fullPath]];
		}
	}
	
	return [self runUnarchiverWithArguments:args];
}

- (NSArray *)listContents
{    
    NSData *data = [self dataByRunningSevenZip];
    NSString *string = [[[NSString alloc] initWithData:data 
		encoding:NSASCIIStringEncoding] autorelease];	
    NSArray *lines = [string componentsSeparatedByString:@"\n"];
    
    // take out first 8 lines (header) and last 2 lines (footer)
	lines = [lines subarrayWithRange:NSMakeRange(17, [lines count] - 17)];
	lines = [lines subarrayWithRange:NSMakeRange(0, [lines count] - 3)];
    
    return [self listSevenZipContents:lines];
}

//------------------------------------------------------------------------------
// creating archives
//------------------------------------------------------------------------------
+ (void)createArchive:(NSString *)archivePath withFiles:(NSArray *)filenames archiveType: (ArchiveType) archiveType
{
        NSEnumerator *filenameCursor;
        NSString *filename;
        NSString *workdir;
        NSMutableArray *arguments;

        // make sure archivePath has the correct suffix
        if ([archivePath hasSuffix:@".7z"] == NO)
          {
            archivePath = [archivePath stringByAppendingString:@".7z"];
          }
        // build arguments for commandline: 7z a filename <list of files>
        arguments = [NSMutableArray array];
        [arguments addObject:@"a"];
        [arguments addObject:archivePath];

        // filenames contains absolute paths, convert them to relative paths. This works
        // because you can select only files/directories below a current directory in
        // GWorkspace so all the files *have* to have a common filesystem root.
        filenameCursor = [filenames objectEnumerator];
        while ((filename = [filenameCursor nextObject]) != nil)
        {
                [arguments addObject:[filename lastPathComponent]];
        }

        // change into this directory when running the task
        workdir = [[filenames objectAtIndex:0] stringByDeletingLastPathComponent];

        [self runArchiverWithArguments:arguments inDirectory:workdir];
}

//------------------------------------------------------------------------------
// private API
//------------------------------------------------------------------------------
- (NSArray *)listSevenZipContents:(NSArray *)lines
{    
    NSEnumerator *cursor;
    NSString *line;
	NSMutableArray *results = [NSMutableArray array];
	    
    cursor = [lines objectEnumerator];
    while ((line = [cursor nextObject]) != nil)
    {
        int length;
        NSString *path, *date, *time, *attributes;
        NSCalendarDate *calendarDate;
        FileInfo *info;
        NSArray *components;

		if ([line length] == 0)
		{
			continue;
		}
		
		components = [line componentsSeparatedByString:@" "];
		components = [components arrayByRemovingEmptyStrings];
		
		// directly skip directory entries. Fortunately, 7zip displays attributes for each file
		attributes = [components objectAtIndex:2];
		if ([attributes characterAtIndex:0] == 'D')
		{
			continue;
		}

		length = [[components objectAtIndex:3] intValue];

		// extract the path, it's always the last element
		path = [[components lastObject] stringByRemovingWhitespaceFromBeginning];
		
		date = [components objectAtIndex:0];
		time = [components objectAtIndex:1];		
        date = [NSString stringWithFormat:@"%@ %@", date, time];
        calendarDate = [NSCalendarDate dateWithString:date calendarFormat:@"%Y-%m-%d %H:%M:%S"];

		info = [FileInfo newWithPath:path date:calendarDate 
			size:[NSNumber numberWithInt:length]];
        [results addObject:info];
    }
    return results;
}

- (NSData *)dataByRunningSevenZip
{
	// l = list
	NSArray *args = [NSArray arrayWithObjects:@"l", @"--", [self path], nil];
	return [self dataByRunningUnachiverWithArguments:args];
}

@end