zipruby 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of zipruby might be problematic. Click here for more details.
- data/ext/extconf.rb +5 -0
- data/ext/zipruby.c +18 -0
- data/ext/zipruby.h +9 -0
- data/ext/zipruby_archive.c +626 -0
- data/ext/zipruby_archive.h +18 -0
- data/ext/zipruby_error.c +10 -0
- data/ext/zipruby_error.h +6 -0
- data/ext/zipruby_file.c +368 -0
- data/ext/zipruby_file.h +23 -0
- data/ext/zipruby_stat.c +164 -0
- data/ext/zipruby_stat.h +12 -0
- data/ext/zipruby_zip.c +37 -0
- data/ext/zipruby_zip.h +6 -0
- data/zipruby.c +1224 -0
- metadata +67 -0
data/ext/extconf.rb
ADDED
data/ext/zipruby.c
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#ifdef _WIN32
|
2
|
+
__declspec(dllexport) void Init_zipruby(void);
|
3
|
+
#endif
|
4
|
+
|
5
|
+
#include "zipruby.h"
|
6
|
+
#include "zipruby_zip.h"
|
7
|
+
#include "zipruby_archive.h"
|
8
|
+
#include "zipruby_file.h"
|
9
|
+
#include "zipruby_stat.h"
|
10
|
+
#include "zipruby_error.h"
|
11
|
+
|
12
|
+
void Init_zipruby() {
|
13
|
+
Init_zipruby_zip();
|
14
|
+
Init_zipruby_archive();
|
15
|
+
Init_zipruby_file();
|
16
|
+
Init_zipruby_stat();
|
17
|
+
Init_zipruby_error();
|
18
|
+
}
|
data/ext/zipruby.h
ADDED
@@ -0,0 +1,626 @@
|
|
1
|
+
#include "zip.h"
|
2
|
+
#include "zipruby.h"
|
3
|
+
#include "zipruby_archive.h"
|
4
|
+
#include "ruby.h"
|
5
|
+
#include "rubyio.h"
|
6
|
+
|
7
|
+
static VALUE zipruby_archive_alloc(VALUE klass);
|
8
|
+
static void zipruby_archive_free(struct zipruby_archive *p);
|
9
|
+
static VALUE zipruby_archive_s_open(int argc, VALUE *argv, VALUE self);
|
10
|
+
static VALUE zipruby_archive_close(VALUE self);
|
11
|
+
static VALUE zipruby_archive_num_files(VALUE self);
|
12
|
+
static VALUE zipruby_archive_get_name(int argc, VALUE *argv, VALUE self);
|
13
|
+
static VALUE zipruby_archive_fopen(int argc, VALUE *argv, VALUE self);
|
14
|
+
static VALUE zipruby_archive_get_stat(int argc, VALUE *argv, VALUE self);
|
15
|
+
static VALUE zipruby_archive_add_buffer(VALUE self, VALUE name, VALUE source);
|
16
|
+
static VALUE zipruby_archive_add_file(int argc, VALUE *argv, VALUE self);
|
17
|
+
static VALUE zipruby_archive_add_filep(int argc, VALUE *argv, VALUE self);
|
18
|
+
static VALUE zipruby_archive_add_zip(VALUE self, VALUE srcarchive);
|
19
|
+
static VALUE zipruby_archive_replace_buffer(VALUE self, VALUE index, VALUE source);
|
20
|
+
static VALUE zipruby_archive_replace_file(VALUE self, VALUE index, VALUE fname);
|
21
|
+
static VALUE zipruby_archive_replace_filep(VALUE self, VALUE index, VALUE file);
|
22
|
+
static VALUE zipruby_archive_get_comment(int argc, VALUE *argv, VALUE self);
|
23
|
+
static VALUE zipruby_archive_set_comment(VALUE self, VALUE comment);
|
24
|
+
static VALUE zipruby_archive_locate_name(int argc, VALUE *argv, VALUE self);
|
25
|
+
static VALUE zipruby_archive_get_fcomment(int argc, VALUE *argv, VALUE self);
|
26
|
+
static VALUE zipruby_archive_set_fcomment(VALUE self, VALUE index, VALUE comment);
|
27
|
+
static VALUE zipruby_archive_fdelete(VALUE self, VALUE index);
|
28
|
+
static VALUE zipruby_archive_frename(VALUE self, VALUE index, VALUE name);
|
29
|
+
static VALUE zipruby_archive_funchange(VALUE self, VALUE index);
|
30
|
+
static VALUE zipruby_archive_funchange_all(VALUE self);
|
31
|
+
static VALUE zipruby_archive_unchange(VALUE self);
|
32
|
+
static VALUE zipruby_archive_revert(VALUE self);
|
33
|
+
static VALUE zipruby_archive_each(VALUE self);
|
34
|
+
|
35
|
+
extern VALUE Zip;
|
36
|
+
VALUE Archive;
|
37
|
+
extern VALUE File;
|
38
|
+
extern VALUE Stat;
|
39
|
+
extern VALUE Error;
|
40
|
+
|
41
|
+
void Init_zipruby_archive() {
|
42
|
+
Archive = rb_define_class_under(Zip, "Archive", rb_cObject);
|
43
|
+
rb_define_alloc_func(Archive, zipruby_archive_alloc);
|
44
|
+
rb_include_module(Archive, rb_mEnumerable);
|
45
|
+
rb_define_singleton_method(Archive, "open", zipruby_archive_s_open, -1);
|
46
|
+
rb_define_method(Archive, "close", zipruby_archive_close, 0);
|
47
|
+
rb_define_method(Archive, "num_files", zipruby_archive_num_files, 0);
|
48
|
+
rb_define_method(Archive, "get_name", zipruby_archive_get_name, -1);
|
49
|
+
rb_define_method(Archive, "fopen", zipruby_archive_fopen, -1);
|
50
|
+
rb_define_method(Archive, "get_stat", zipruby_archive_get_stat, -1);
|
51
|
+
rb_define_method(Archive, "add_buffer", zipruby_archive_add_buffer, 2);
|
52
|
+
rb_define_method(Archive, "add_file", zipruby_archive_add_file, -1);
|
53
|
+
rb_define_method(Archive, "add_filep", zipruby_archive_add_filep, -1);
|
54
|
+
rb_define_method(Archive, "add_zip", zipruby_archive_add_zip, 1);
|
55
|
+
rb_define_method(Archive, "replace_buffer", zipruby_archive_replace_buffer, 2);
|
56
|
+
rb_define_method(Archive, "replace_file", zipruby_archive_replace_file, 2);
|
57
|
+
rb_define_method(Archive, "replace_filep", zipruby_archive_replace_filep, 2);
|
58
|
+
rb_define_method(Archive, "<<", zipruby_archive_add_filep, -1);
|
59
|
+
rb_define_method(Archive, "get_comment", zipruby_archive_get_comment, -1);
|
60
|
+
rb_define_method(Archive, "comment", zipruby_archive_get_comment, -1);
|
61
|
+
rb_define_method(Archive, "comment=", zipruby_archive_set_comment, 1);
|
62
|
+
rb_define_method(Archive, "locate_name", zipruby_archive_locate_name, -1);
|
63
|
+
rb_define_method(Archive, "get_fcomment", zipruby_archive_get_fcomment, -1);
|
64
|
+
rb_define_method(Archive, "set_fcomment", zipruby_archive_set_fcomment, 2);
|
65
|
+
rb_define_method(Archive, "fdelete", zipruby_archive_fdelete, 1);
|
66
|
+
rb_define_method(Archive, "frename", zipruby_archive_frename, 2);
|
67
|
+
rb_define_method(Archive, "funchange", zipruby_archive_funchange, 1);
|
68
|
+
rb_define_method(Archive, "funchange_all", zipruby_archive_funchange_all, 0);
|
69
|
+
rb_define_method(Archive, "unchange", zipruby_archive_unchange, 0);
|
70
|
+
rb_define_method(Archive, "frevert", zipruby_archive_unchange, 1);
|
71
|
+
rb_define_method(Archive, "revert", zipruby_archive_revert, 0);
|
72
|
+
rb_define_method(Archive, "each", zipruby_archive_each, 0);
|
73
|
+
}
|
74
|
+
|
75
|
+
static VALUE zipruby_archive_alloc(VALUE klass) {
|
76
|
+
struct zipruby_archive *p = ALLOC(struct zipruby_archive);
|
77
|
+
|
78
|
+
p->archive = NULL;
|
79
|
+
|
80
|
+
return Data_Wrap_Struct(klass, 0, zipruby_archive_free, p);
|
81
|
+
}
|
82
|
+
|
83
|
+
static void zipruby_archive_free(struct zipruby_archive *p) {
|
84
|
+
xfree(p);
|
85
|
+
}
|
86
|
+
|
87
|
+
/* */
|
88
|
+
static VALUE zipruby_archive_s_open(int argc, VALUE *argv, VALUE self) {
|
89
|
+
VALUE path, flags;
|
90
|
+
VALUE archive;
|
91
|
+
struct zipruby_archive *p_archive;
|
92
|
+
int i_flags = 0;
|
93
|
+
int errorp;
|
94
|
+
|
95
|
+
rb_scan_args(argc, argv, "11", &path, &flags);
|
96
|
+
Check_Type(path, T_STRING);
|
97
|
+
|
98
|
+
if (!NIL_P(flags)) {
|
99
|
+
i_flags = NUM2INT(flags);
|
100
|
+
}
|
101
|
+
|
102
|
+
archive = rb_funcall(Archive, rb_intern("new"), 0);
|
103
|
+
Data_Get_Struct(archive, struct zipruby_archive, p_archive);
|
104
|
+
|
105
|
+
if ((p_archive->archive = zip_open(StringValuePtr(path), i_flags, &errorp)) == NULL) {
|
106
|
+
char errstr[ERRSTR_BUFSIZE];
|
107
|
+
zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
|
108
|
+
rb_raise(Error, "Open archive failed - %s: %s", StringValuePtr(path), errstr);
|
109
|
+
}
|
110
|
+
|
111
|
+
if (rb_block_given_p()) {
|
112
|
+
VALUE retval;
|
113
|
+
int status;
|
114
|
+
|
115
|
+
retval = rb_protect(rb_yield, archive, &status);
|
116
|
+
zipruby_archive_close(archive);
|
117
|
+
|
118
|
+
if (status != 0) {
|
119
|
+
rb_jump_tag(status);
|
120
|
+
}
|
121
|
+
|
122
|
+
return retval;
|
123
|
+
} else {
|
124
|
+
return archive;
|
125
|
+
}
|
126
|
+
};
|
127
|
+
|
128
|
+
/* */
|
129
|
+
static VALUE zipruby_archive_close(VALUE self) {
|
130
|
+
struct zipruby_archive *p_archive;
|
131
|
+
|
132
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
133
|
+
Check_Archive(p_archive);
|
134
|
+
|
135
|
+
if (zip_close(p_archive->archive) == -1) {
|
136
|
+
zip_unchange_all(p_archive->archive);
|
137
|
+
zip_unchange_archive(p_archive->archive);
|
138
|
+
rb_raise(Error, "Close archive failed: %s", zip_strerror(p_archive->archive));
|
139
|
+
}
|
140
|
+
|
141
|
+
p_archive->archive = NULL;
|
142
|
+
|
143
|
+
return Qnil;
|
144
|
+
}
|
145
|
+
|
146
|
+
/* */
|
147
|
+
static VALUE zipruby_archive_num_files(VALUE self) {
|
148
|
+
struct zipruby_archive *p_archive;
|
149
|
+
int num_files;
|
150
|
+
|
151
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
152
|
+
Check_Archive(p_archive);
|
153
|
+
num_files = zip_get_num_files(p_archive->archive);
|
154
|
+
|
155
|
+
return INT2NUM(num_files);
|
156
|
+
}
|
157
|
+
|
158
|
+
/* */
|
159
|
+
static VALUE zipruby_archive_get_name(int argc, VALUE *argv, VALUE self) {
|
160
|
+
VALUE index, flags;
|
161
|
+
struct zipruby_archive *p_archive;
|
162
|
+
int i_flags = 0;
|
163
|
+
const char *name;
|
164
|
+
|
165
|
+
rb_scan_args(argc, argv, "11", &index, &flags);
|
166
|
+
Check_Type(index, T_FIXNUM);
|
167
|
+
|
168
|
+
if (!NIL_P(flags)) {
|
169
|
+
i_flags = NUM2INT(flags);
|
170
|
+
}
|
171
|
+
|
172
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
173
|
+
Check_Archive(p_archive);
|
174
|
+
|
175
|
+
if ((name = zip_get_name(p_archive->archive, NUM2INT(index), i_flags)) == NULL) {
|
176
|
+
rb_raise(Error, "Get name failed: %s", zip_strerror(p_archive->archive));
|
177
|
+
}
|
178
|
+
|
179
|
+
return (name != NULL) ? rb_str_new2(name) : Qnil;
|
180
|
+
}
|
181
|
+
|
182
|
+
/* */
|
183
|
+
static VALUE zipruby_archive_fopen(int argc, VALUE *argv, VALUE self) {
|
184
|
+
VALUE index, flags, stat_flags, file;
|
185
|
+
|
186
|
+
rb_scan_args(argc, argv, "12", &index, &flags, &stat_flags);
|
187
|
+
file = rb_funcall(File, rb_intern("new"), 4, self, index, flags, stat_flags);
|
188
|
+
|
189
|
+
if (rb_block_given_p()) {
|
190
|
+
VALUE retval;
|
191
|
+
int status;
|
192
|
+
|
193
|
+
retval = rb_protect(rb_yield, file, &status);
|
194
|
+
rb_funcall(file, rb_intern("close"), 0);
|
195
|
+
|
196
|
+
if (status != 0) {
|
197
|
+
rb_jump_tag(status);
|
198
|
+
}
|
199
|
+
|
200
|
+
return retval;
|
201
|
+
} else {
|
202
|
+
return file;
|
203
|
+
}
|
204
|
+
}
|
205
|
+
|
206
|
+
/* */
|
207
|
+
static VALUE zipruby_archive_get_stat(int argc, VALUE *argv, VALUE self) {
|
208
|
+
VALUE index, flags;
|
209
|
+
|
210
|
+
rb_scan_args(argc, argv, "11", &index, &flags);
|
211
|
+
|
212
|
+
return rb_funcall(Stat, rb_intern("new"), 3, self, index, flags);
|
213
|
+
}
|
214
|
+
|
215
|
+
/* */
|
216
|
+
static VALUE zipruby_archive_add_buffer(VALUE self, VALUE name, VALUE source) {
|
217
|
+
struct zipruby_archive *p_archive;
|
218
|
+
struct zip_source *zsource;
|
219
|
+
char *data;
|
220
|
+
off_t len;
|
221
|
+
|
222
|
+
Check_Type(name, T_STRING);
|
223
|
+
Check_Type(source, T_STRING);
|
224
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
225
|
+
Check_Archive(p_archive);
|
226
|
+
|
227
|
+
len = RSTRING(source)->len;
|
228
|
+
|
229
|
+
if ((data = malloc(len)) == NULL) {
|
230
|
+
rb_raise(rb_eRuntimeError, "Add file failed: Cannot allocate memory");
|
231
|
+
}
|
232
|
+
|
233
|
+
memset(data, 0, len);
|
234
|
+
memcpy(data, StringValuePtr(source), len);
|
235
|
+
|
236
|
+
if ((zsource = zip_source_buffer(p_archive->archive, data, len, 1)) == NULL) {
|
237
|
+
free(data);
|
238
|
+
rb_raise(Error, "Add file failed - %s: %s", StringValuePtr(name), zip_strerror(p_archive->archive));
|
239
|
+
}
|
240
|
+
|
241
|
+
if (zip_add(p_archive->archive, StringValuePtr(name), zsource) == -1) {
|
242
|
+
zip_source_free(zsource);
|
243
|
+
zip_unchange_all(p_archive->archive);
|
244
|
+
zip_unchange_archive(p_archive->archive);
|
245
|
+
rb_raise(Error, "Add file failed - %s: %s", StringValuePtr(name), zip_strerror(p_archive->archive));
|
246
|
+
}
|
247
|
+
|
248
|
+
return Qnil;
|
249
|
+
}
|
250
|
+
|
251
|
+
/* */
|
252
|
+
static VALUE zipruby_archive_replace_buffer(VALUE self, VALUE index, VALUE source) {
|
253
|
+
struct zipruby_archive *p_archive;
|
254
|
+
struct zip_source *zsource;
|
255
|
+
char *data;
|
256
|
+
off_t len;
|
257
|
+
|
258
|
+
Check_Type(index, T_FIXNUM);
|
259
|
+
Check_Type(source, T_STRING);
|
260
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
261
|
+
Check_Archive(p_archive);
|
262
|
+
|
263
|
+
len = RSTRING(source)->len;
|
264
|
+
data = malloc(len);
|
265
|
+
memcpy(data, StringValuePtr(source), len);
|
266
|
+
|
267
|
+
if ((zsource = zip_source_buffer(p_archive->archive, data, len, 1)) == NULL) {
|
268
|
+
free(data);
|
269
|
+
rb_raise(Error, "Replace file failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
|
270
|
+
}
|
271
|
+
|
272
|
+
if (zip_replace(p_archive->archive, NUM2INT(index), zsource) == -1) {
|
273
|
+
zip_source_free(zsource);
|
274
|
+
zip_unchange_all(p_archive->archive);
|
275
|
+
zip_unchange_archive(p_archive->archive);
|
276
|
+
rb_raise(Error, "Replace file failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
|
277
|
+
}
|
278
|
+
|
279
|
+
return Qnil;
|
280
|
+
}
|
281
|
+
|
282
|
+
/* */
|
283
|
+
static VALUE zipruby_archive_add_file(int argc, VALUE *argv, VALUE self) {
|
284
|
+
VALUE name, fname;
|
285
|
+
struct zipruby_archive *p_archive;
|
286
|
+
struct zip_source *zsource;
|
287
|
+
|
288
|
+
rb_scan_args(argc, argv, "11", &name, &fname);
|
289
|
+
|
290
|
+
|
291
|
+
if (NIL_P(fname)) {
|
292
|
+
fname = name;
|
293
|
+
name = Qnil;
|
294
|
+
}
|
295
|
+
|
296
|
+
if (NIL_P(name)) {
|
297
|
+
name = rb_funcall(rb_cFile, rb_intern("basename"), 1, fname);
|
298
|
+
}
|
299
|
+
|
300
|
+
Check_Type(name, T_STRING);
|
301
|
+
Check_Type(fname, T_STRING);
|
302
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
303
|
+
Check_Archive(p_archive);
|
304
|
+
|
305
|
+
if ((zsource = zip_source_file(p_archive->archive, StringValuePtr(fname), 0, -1)) == NULL) {
|
306
|
+
rb_raise(Error, "Add file failed - %s: %s", StringValuePtr(name), zip_strerror(p_archive->archive));
|
307
|
+
}
|
308
|
+
|
309
|
+
if (zip_add(p_archive->archive, StringValuePtr(name), zsource) == -1) {
|
310
|
+
zip_source_free(zsource);
|
311
|
+
zip_unchange_all(p_archive->archive);
|
312
|
+
zip_unchange_archive(p_archive->archive);
|
313
|
+
rb_raise(Error, "Add file failed - %s: %s", StringValuePtr(name), zip_strerror(p_archive->archive));
|
314
|
+
}
|
315
|
+
|
316
|
+
return Qnil;
|
317
|
+
}
|
318
|
+
|
319
|
+
/* */
|
320
|
+
static VALUE zipruby_archive_replace_file(VALUE self, VALUE index, VALUE fname) {
|
321
|
+
struct zipruby_archive *p_archive;
|
322
|
+
struct zip_source *zsource;
|
323
|
+
|
324
|
+
Check_Type(index, T_FIXNUM);
|
325
|
+
Check_Type(fname, T_STRING);
|
326
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
327
|
+
Check_Archive(p_archive);
|
328
|
+
|
329
|
+
if ((zsource = zip_source_file(p_archive->archive, StringValuePtr(fname), 0, -1)) == NULL) {
|
330
|
+
rb_raise(Error, "Replace file failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
|
331
|
+
}
|
332
|
+
|
333
|
+
if (zip_replace(p_archive->archive, NUM2INT(index), zsource) == -1) {
|
334
|
+
zip_source_free(zsource);
|
335
|
+
zip_unchange_all(p_archive->archive);
|
336
|
+
zip_unchange_archive(p_archive->archive);
|
337
|
+
rb_raise(Error, "Replace file failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
|
338
|
+
}
|
339
|
+
|
340
|
+
return Qnil;
|
341
|
+
}
|
342
|
+
|
343
|
+
/* */
|
344
|
+
static VALUE zipruby_archive_add_filep(int argc, VALUE *argv, VALUE self) {
|
345
|
+
VALUE name, file, source;
|
346
|
+
|
347
|
+
rb_scan_args(argc, argv, "11", &name, &file);
|
348
|
+
|
349
|
+
if (NIL_P(file)) {
|
350
|
+
file = name;
|
351
|
+
name = Qnil;
|
352
|
+
}
|
353
|
+
|
354
|
+
Check_Type(file, T_FILE);
|
355
|
+
|
356
|
+
if (NIL_P(name)) {
|
357
|
+
name = rb_funcall(rb_cFile, rb_intern("basename"), 1, rb_funcall(file, rb_intern("path"), 0));
|
358
|
+
}
|
359
|
+
|
360
|
+
source = rb_funcall(file, rb_intern("read"), 0);
|
361
|
+
|
362
|
+
return zipruby_archive_add_buffer(self, name, source);
|
363
|
+
}
|
364
|
+
|
365
|
+
/* */
|
366
|
+
static VALUE zipruby_archive_replace_filep(VALUE self, VALUE index, VALUE file) {
|
367
|
+
VALUE source;
|
368
|
+
|
369
|
+
Check_Type(file, T_FILE);
|
370
|
+
source = rb_funcall(file, rb_intern("read"), 0);
|
371
|
+
|
372
|
+
return zipruby_archive_replace_buffer(self, index, source);
|
373
|
+
}
|
374
|
+
|
375
|
+
/* */
|
376
|
+
static VALUE zipruby_archive_get_comment(int argc, VALUE *argv, VALUE self) {
|
377
|
+
VALUE flags;
|
378
|
+
struct zipruby_archive *p_archive;
|
379
|
+
const char *comment;
|
380
|
+
int lenp, i_flags = 0;
|
381
|
+
|
382
|
+
rb_scan_args(argc, argv, "01", &flags);
|
383
|
+
|
384
|
+
if (!NIL_P(flags)) {
|
385
|
+
i_flags = NUM2INT(flags);
|
386
|
+
}
|
387
|
+
|
388
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
389
|
+
Check_Archive(p_archive);
|
390
|
+
|
391
|
+
// XXX: How is the error checked?
|
392
|
+
comment = zip_get_archive_comment(p_archive->archive, &lenp, i_flags);
|
393
|
+
|
394
|
+
return comment ? rb_str_new(comment, lenp) : Qnil;
|
395
|
+
}
|
396
|
+
|
397
|
+
/* */
|
398
|
+
static VALUE zipruby_archive_add_zip(VALUE self, VALUE srcarchive) {
|
399
|
+
struct zipruby_archive *p_archive, *p_srcarchive;
|
400
|
+
struct zip_source *zsource;
|
401
|
+
int i, num_files;
|
402
|
+
|
403
|
+
if (!rb_obj_is_instance_of(srcarchive, Archive)) {
|
404
|
+
rb_raise(rb_eTypeError, "wrong argument type %s (expected Zip::Archive)", rb_class2name(CLASS_OF(srcarchive)));
|
405
|
+
}
|
406
|
+
|
407
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
408
|
+
Check_Archive(p_archive);
|
409
|
+
Data_Get_Struct(srcarchive, struct zipruby_archive, p_srcarchive);
|
410
|
+
Check_Archive(p_srcarchive);
|
411
|
+
|
412
|
+
num_files = zip_get_num_files(p_srcarchive->archive);
|
413
|
+
|
414
|
+
for (i = 0; i < num_files; i++) {
|
415
|
+
if ((zsource = zip_source_zip(p_archive->archive, p_srcarchive->archive, i, 0, 0, -1)) == NULL) {
|
416
|
+
rb_raise(Error, "Add zip failed: %s", zip_strerror(p_archive->archive));
|
417
|
+
}
|
418
|
+
|
419
|
+
if (zip_add(p_archive->archive, zip_get_name(p_srcarchive->archive, i, 0), zsource) == -1) {
|
420
|
+
zip_source_free(zsource);
|
421
|
+
zip_unchange_all(p_archive->archive);
|
422
|
+
zip_unchange_archive(p_archive->archive);
|
423
|
+
rb_raise(Error, "Add zip failed: %s", zip_strerror(p_archive->archive));
|
424
|
+
}
|
425
|
+
}
|
426
|
+
|
427
|
+
return Qnil;
|
428
|
+
}
|
429
|
+
|
430
|
+
/* */
|
431
|
+
static VALUE zipruby_archive_set_comment(VALUE self, VALUE comment) {
|
432
|
+
struct zipruby_archive *p_archive;
|
433
|
+
const char *s_comment = NULL;
|
434
|
+
int len = 0;
|
435
|
+
|
436
|
+
if (!NIL_P(comment)) {
|
437
|
+
Check_Type(comment, T_STRING);
|
438
|
+
s_comment = StringValuePtr(comment);
|
439
|
+
len = RSTRING(comment)->len;
|
440
|
+
}
|
441
|
+
|
442
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
443
|
+
Check_Archive(p_archive);
|
444
|
+
|
445
|
+
if (zip_set_archive_comment(p_archive->archive, s_comment, len) == -1) {
|
446
|
+
zip_unchange_all(p_archive->archive);
|
447
|
+
zip_unchange_archive(p_archive->archive);
|
448
|
+
rb_raise(Error, "Comment archived failed: %s", zip_strerror(p_archive->archive));
|
449
|
+
}
|
450
|
+
|
451
|
+
return Qnil;
|
452
|
+
}
|
453
|
+
|
454
|
+
/* */
|
455
|
+
static VALUE zipruby_archive_locate_name(int argc, VALUE *argv, VALUE self) {
|
456
|
+
VALUE fname, flags;
|
457
|
+
struct zipruby_archive *p_archive;
|
458
|
+
int i_flags = 0;
|
459
|
+
|
460
|
+
rb_scan_args(argc, argv, "11", &fname, &flags);
|
461
|
+
Check_Type(fname, T_STRING);
|
462
|
+
|
463
|
+
if (!NIL_P(flags)) {
|
464
|
+
i_flags = NUM2INT(flags);
|
465
|
+
}
|
466
|
+
|
467
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
468
|
+
Check_Archive(p_archive);
|
469
|
+
|
470
|
+
return INT2NUM(zip_name_locate(p_archive->archive, StringValuePtr(fname), i_flags));
|
471
|
+
}
|
472
|
+
|
473
|
+
/* */
|
474
|
+
static VALUE zipruby_archive_get_fcomment(int argc, VALUE *argv, VALUE self) {
|
475
|
+
VALUE index, flags;
|
476
|
+
struct zipruby_archive *p_archive;
|
477
|
+
const char *comment;
|
478
|
+
int lenp, i_flags = 0;
|
479
|
+
|
480
|
+
rb_scan_args(argc, argv, "11", &index, &flags);
|
481
|
+
|
482
|
+
if (!NIL_P(flags)) {
|
483
|
+
i_flags = NUM2INT(flags);
|
484
|
+
}
|
485
|
+
|
486
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
487
|
+
Check_Archive(p_archive);
|
488
|
+
|
489
|
+
// XXX: How is the error checked?
|
490
|
+
comment = zip_get_file_comment(p_archive->archive, NUM2INT(index), &lenp, i_flags);
|
491
|
+
|
492
|
+
return comment ? rb_str_new(comment, lenp) : Qnil;
|
493
|
+
}
|
494
|
+
|
495
|
+
/* */
|
496
|
+
static VALUE zipruby_archive_set_fcomment(VALUE self, VALUE index, VALUE comment) {
|
497
|
+
struct zipruby_archive *p_archive;
|
498
|
+
char *s_comment = NULL;
|
499
|
+
int len = 0;
|
500
|
+
|
501
|
+
if (!NIL_P(comment)) {
|
502
|
+
Check_Type(comment, T_STRING);
|
503
|
+
s_comment = StringValuePtr(comment);
|
504
|
+
len = RSTRING(comment)->len;
|
505
|
+
}
|
506
|
+
|
507
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
508
|
+
Check_Archive(p_archive);
|
509
|
+
|
510
|
+
if (zip_set_file_comment(p_archive->archive, NUM2INT(index), s_comment, len) == -1) {
|
511
|
+
zip_unchange_all(p_archive->archive);
|
512
|
+
zip_unchange_archive(p_archive->archive);
|
513
|
+
rb_raise(Error, "Comment file failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
|
514
|
+
}
|
515
|
+
|
516
|
+
return Qnil;
|
517
|
+
}
|
518
|
+
|
519
|
+
/* */
|
520
|
+
static VALUE zipruby_archive_fdelete(VALUE self, VALUE index) {
|
521
|
+
struct zipruby_archive *p_archive;
|
522
|
+
|
523
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
524
|
+
Check_Archive(p_archive);
|
525
|
+
|
526
|
+
if (zip_delete(p_archive->archive, NUM2INT(index)) == -1) {
|
527
|
+
zip_unchange_all(p_archive->archive);
|
528
|
+
zip_unchange_archive(p_archive->archive);
|
529
|
+
rb_raise(Error, "Delete file failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
|
530
|
+
}
|
531
|
+
|
532
|
+
return Qnil;
|
533
|
+
}
|
534
|
+
|
535
|
+
/* */
|
536
|
+
static VALUE zipruby_archive_frename(VALUE self, VALUE index, VALUE name) {
|
537
|
+
struct zipruby_archive *p_archive;
|
538
|
+
|
539
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
540
|
+
Check_Archive(p_archive);
|
541
|
+
|
542
|
+
if (zip_rename(p_archive->archive, NUM2INT(index), StringValuePtr(name)) == -1) {
|
543
|
+
zip_unchange_all(p_archive->archive);
|
544
|
+
zip_unchange_archive(p_archive->archive);
|
545
|
+
rb_raise(Error, "Rename file failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
|
546
|
+
}
|
547
|
+
|
548
|
+
return Qnil;
|
549
|
+
}
|
550
|
+
|
551
|
+
/* */
|
552
|
+
static VALUE zipruby_archive_funchange(VALUE self, VALUE index) {
|
553
|
+
struct zipruby_archive *p_archive;
|
554
|
+
|
555
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
556
|
+
Check_Archive(p_archive);
|
557
|
+
|
558
|
+
if (zip_unchange(p_archive->archive, NUM2INT(index)) == -1) {
|
559
|
+
zip_unchange_all(p_archive->archive);
|
560
|
+
zip_unchange_archive(p_archive->archive);
|
561
|
+
rb_raise(Error, "Unchange file failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
|
562
|
+
}
|
563
|
+
|
564
|
+
return Qnil;
|
565
|
+
}
|
566
|
+
|
567
|
+
/* */
|
568
|
+
static VALUE zipruby_archive_funchange_all(VALUE self) {
|
569
|
+
struct zipruby_archive *p_archive;
|
570
|
+
|
571
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
572
|
+
Check_Archive(p_archive);
|
573
|
+
|
574
|
+
if (zip_unchange_all(p_archive->archive) == -1) {
|
575
|
+
rb_raise(Error, "Unchange all file failed: %s", zip_strerror(p_archive->archive));
|
576
|
+
}
|
577
|
+
|
578
|
+
return Qnil;
|
579
|
+
}
|
580
|
+
|
581
|
+
/* */
|
582
|
+
static VALUE zipruby_archive_unchange(VALUE self) {
|
583
|
+
struct zipruby_archive *p_archive;
|
584
|
+
|
585
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
586
|
+
Check_Archive(p_archive);
|
587
|
+
|
588
|
+
if (zip_unchange_archive(p_archive->archive) == -1) {
|
589
|
+
rb_raise(Error, "Unchange archive failed: %s", zip_strerror(p_archive->archive));
|
590
|
+
}
|
591
|
+
|
592
|
+
return Qnil;
|
593
|
+
}
|
594
|
+
|
595
|
+
/* */
|
596
|
+
static VALUE zipruby_archive_revert(VALUE self) {
|
597
|
+
zipruby_archive_funchange_all(self);
|
598
|
+
zipruby_archive_unchange(self);
|
599
|
+
|
600
|
+
return Qnil;
|
601
|
+
}
|
602
|
+
|
603
|
+
/* */
|
604
|
+
static VALUE zipruby_archive_each(VALUE self) {
|
605
|
+
struct zipruby_archive *p_archive;
|
606
|
+
int i, num_files;
|
607
|
+
|
608
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
609
|
+
Check_Archive(p_archive);
|
610
|
+
num_files = zip_get_num_files(p_archive->archive);
|
611
|
+
|
612
|
+
for (i = 0; i < num_files; i++) {
|
613
|
+
VALUE file;
|
614
|
+
int status;
|
615
|
+
|
616
|
+
file = rb_funcall(File, rb_intern("new"), 2, self, INT2NUM(i));
|
617
|
+
rb_protect(rb_yield, file, &status);
|
618
|
+
rb_funcall(file, rb_intern("close"), 0);
|
619
|
+
|
620
|
+
if (status != 0) {
|
621
|
+
rb_jump_tag(status);
|
622
|
+
}
|
623
|
+
}
|
624
|
+
|
625
|
+
return Qnil;
|
626
|
+
}
|
@@ -0,0 +1,18 @@
|
|
1
|
+
#ifndef __ZIPRUBY_ARCHIVE_H__
|
2
|
+
#define __ZIPRUBY_ARCHIVE_H__
|
3
|
+
|
4
|
+
#include "zip.h"
|
5
|
+
|
6
|
+
struct zipruby_archive {
|
7
|
+
struct zip *archive;
|
8
|
+
};
|
9
|
+
|
10
|
+
void Init_zipruby_archive();
|
11
|
+
|
12
|
+
#define Check_Archive(p) do { \
|
13
|
+
if ((p)->archive == NULL) { \
|
14
|
+
rb_raise(rb_eRuntimeError, "invalid Zip::Archive"); \
|
15
|
+
} \
|
16
|
+
} while(0)
|
17
|
+
|
18
|
+
#endif
|
data/ext/zipruby_error.c
ADDED