-
Notifications
You must be signed in to change notification settings - Fork 10
/
XADWrapper.m
172 lines (145 loc) · 3.68 KB
/
XADWrapper.m
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
//
// XADWrapper.m
// cooViewer
//
// Created by coo on 08/01/20.
// Copyright 2008 coo. All rights reserved.
//
#import "XADWrapper.h"
#import "XADItem.h"
@implementation XADWrapper
-(id)initWithPath:(NSString*)path{
self = [super init];
if (self) {
archive = [[XADArchive alloc] initWithFile:path];
contentArray = [[NSMutableArray array] retain];
contentIndexArray = [[NSMutableArray array] retain];
filePath=[path retain];
password=nil;
int i;
for (i=0; i<[archive numberOfEntries]; i++) {
[contentIndexArray addObject:[archive nameOfEntry:i]];
if (![archive entryIsDirectory:i] && [archive sizeOfEntry:i] != 0) {
[contentArray addObject:[[XADItem alloc] initWithPath:[archive nameOfEntry:i] andWrapper:self]];
} else {
//NSLog(@"isdir %@",[archive nameOfEntry:i]);
}
}
}
return self;
}
-(id)initWithPath:(NSString*)path nameEncoding:(NSStringEncoding)enc{
self = [super init];
if (self) {
archive = [[XADArchive alloc] initWithFile:path];
contentArray = [[NSMutableArray array] retain];
contentIndexArray = [[NSMutableArray array] retain];
filePath=[path retain];
if (enc) [archive setNameEncoding:enc];
password=nil;
if ([archive nameOfEntry:0] == nil) {
[archive setNameEncoding:nil];
}
int i;
for (i=0; i<[archive numberOfEntries]; i++) {
[contentIndexArray addObject:[archive nameOfEntry:i]];
if (![archive entryIsDirectory:i] && [archive sizeOfEntry:i] != 0) {
[contentArray addObject:[[XADItem alloc] initWithPath:[archive nameOfEntry:i] andWrapper:self]];
} else {
//NSLog(@"isdir %@",[archive nameOfEntry:i]);
}
}
}
return self;
}
- (void)dealloc
{
[archive init];
if(archive)[archive release];
if(filePath)[filePath release];
if(contentArray)[contentArray release];
if(contentIndexArray)[contentIndexArray release];
[super dealloc];
}
//access
-(int)itemCount
{
return [archive numberOfEntries];
}
-(int)xadIndexOfName:(NSString *)name
{
return (int)[contentIndexArray indexOfObject:name];
}
-(id)itemForPath:(NSString *)path
{
//NSLog(@"%@ %i",path,[archive _entryIndexOfName:path]);
return [archive contentsOfEntry:[self xadIndexOfName:path]];
}
-(id)itemAtIndex:(int)index
{
//使ってない
//return [archive contentsOfEntry:index];
return [self itemForPath:[[contentArray objectAtIndex:index] path]];
}
-(id)itemArray
{
return contentArray;
}
-(id)contents
{
return contentArray;
}
//ファイルパス
-(NSString *)filePath
{
return filePath;
}
-(BOOL)crypted
{
if ([archive isEncrypted]) {
return YES;
}
return NO;
}
-(NSString *)password
{
return password;
}
-(void)setPassword:(NSString *)inStr
{
if(password)[password release];
password=nil;
if(inStr){
password=[inStr retain];
[archive setPassword:password];
}
}
//パスワードを設定してあっていればYES,間違ってればNOを返す
-(BOOL)checkAndSetPassword:(NSString *)newPassword
{
if ([self itemCount]==0) return NO;
[self setPassword:newPassword];
NSData *temp = [self itemAtIndex:0];
//NSLog(@"%@",[archive describeLastError]);
if (temp) {
return YES;
}
return NO;
}
-(XADArchive*)archive
{
return archive;
}
-(NSStringEncoding)encoding
{
return [archive nameEncoding];
}
-(BOOL)uncompress:(int)index toTempDir:(NSString*)dir
{
return [archive extractEntry:[self xadIndexOfName:[[contentArray objectAtIndex:index] path]] to:dir];
}
-(BOOL)uncompress:(int)index as:(NSString*)fileName
{
return [archive _extractEntry:[self xadIndexOfName:[[contentArray objectAtIndex:index] path]] as:fileName deferDirectories:NO dataFork:YES resourceFork:YES];
}
@end