zipruby 0.3.0-mswin32 → 0.3.1-mswin32

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.

Files changed (5) hide show
  1. data/ChangeLog +10 -0
  2. data/README.txt +72 -24
  3. data/lib/i386-mswin32/zipruby.so +0 -0
  4. data/zipruby.c +314 -53
  5. metadata +4 -2
data/ChangeLog ADDED
@@ -0,0 +1,10 @@
1
+ 2009-05-16 winebarrel
2
+
3
+ * improve Zip::Archive#open_buffer, add_io, replace_io, replace_function
4
+
5
+ * fixes bug: Zip::Archive#add_io, <<
6
+
7
+ * add method: Zip::Archive#add_dir
8
+
9
+ * zipruby/README.txt: fixes example code.
10
+
data/README.txt CHANGED
@@ -68,32 +68,36 @@ https://rubyforge.org/frs/?group_id=6124
68
68
 
69
69
  require 'zipruby'
70
70
 
71
+ bar_txt = open('bar.txt')
72
+
71
73
  Zip::Archive.open('filename.zip', Zip::CREATE) do |ar|
72
- ar.add_file('foo.txt') # add file to zip archive
74
+ # if overwrite: ..., Zip::CREATE | Zip::TRUNC) do |ar|
75
+
76
+ ar.add_file('foo.txt') # add file to zip archive
73
77
 
74
78
  # add file to zip archive from File object
75
- open('bar.txt') do |f|
76
- ar << f # or ar.add_io(f)
77
- end
79
+ ar << bar_txt # or ar.add_io(bar_txt)
78
80
 
79
81
  # add file to zip archive from buffer
80
82
  ar.add_buffer('zoo.txt', 'Hello, world!')
81
83
  end
82
84
 
85
+ bar_txt.rewind
86
+
83
87
  # include directory in zip archive
84
88
  Zip::Archive.open('filename.zip') do |ar|
85
89
  ar.add_file('dirname/foo.txt', 'foo.txt')
86
- # args: <entry name>, <source>
90
+ # args: <entry name> , <source>
87
91
 
88
- open('bar.txt') do |f|
89
- ar.add_io('dirname/bar.txt', f)
90
- # args: <entry name>, <source>
91
- end
92
+ ar.add_io('dirname/bar.txt', bar_txt)
93
+ # args: <entry name> , <source>
92
94
 
93
95
  ar.add_buffer('dirname/zoo.txt', 'Hello, world!')
94
- # args: <entry name>, <source>
96
+ # args: <entry name> , <source>
95
97
  end
96
98
 
99
+ bar_txt.close # close file after archive closed
100
+
97
101
  # add huge file
98
102
  source = %w(London Bridge is falling down)
99
103
 
@@ -108,18 +112,18 @@ https://rubyforge.org/frs/?group_id=6124
108
112
 
109
113
  require 'zipruby'
110
114
 
115
+ bar_txt = open('bar.txt')
116
+
111
117
  Zip::Archive.open('filename.zip') do |ar|
112
118
  # replace file in zip archive
113
119
  ar.replace_file(0, 'foo.txt')
114
120
 
115
121
  # replace file in zip archive with File object
116
- open('bar.txt') do |f|
117
- ar.replace_io(1, f)
118
- end
119
-
122
+ ar.replace_io(1, bar_txt)
123
+
120
124
  # if commit changes
121
125
  # ar.commit
122
-
126
+
123
127
  # replace file in zip archive with buffer
124
128
  ar.replace_buffer(2, 'Hello, world!')
125
129
  # or
@@ -128,7 +132,7 @@ https://rubyforge.org/frs/?group_id=6124
128
132
  # ar.replace_buffer('entry name', 'Hello, world!', Zip::FL_NOCASE)
129
133
 
130
134
  # add or replace file in zip archive
131
- ar.add_or_replace_file(3, 'foo.txt')
135
+ ar.add_or_replace_file('zoo.txt', 'foo.txt')
132
136
  end
133
137
 
134
138
  # append comment
@@ -140,6 +144,8 @@ https://rubyforge.org/frs/?group_id=6124
140
144
  EOS
141
145
  end
142
146
 
147
+ bar_txt.close # close file after archive closed
148
+
143
149
  # ar1 import ar2 entries
144
150
  Zip::Archive.open('ar1.zip') do |ar1|
145
151
  Zip::Archive.open('ar2.zip') do |ar2|
@@ -152,16 +158,16 @@ https://rubyforge.org/frs/?group_id=6124
152
158
  require 'zipruby'
153
159
 
154
160
  # encrypt
155
- Zip::Archive.encrypt('filename.zip', 'password')
161
+ Zip::Archive.encrypt('filename.zip', 'password') # return true if encrypted
156
162
  # or
157
- # Zip::Archive.encrypt('filename.zip') do |ar|
163
+ # Zip::Archive.open('filename.zip') do |ar|
158
164
  # ar.encrypt('password')
159
165
  # end
160
166
 
161
167
  # decrypt
162
- Zip::Archive.decrypt('filename.zip', 'password')
168
+ Zip::Archive.decrypt('filename.zip', 'password') # return true if encrypted
163
169
  # or
164
- # Zip::Archive.decrypt('filename.zip') do |ar|
170
+ # Zip::Archive.open('filename.zip') do |ar|
165
171
  # ar.decrypt('password')
166
172
  # end
167
173
 
@@ -182,10 +188,14 @@ https://rubyforge.org/frs/?group_id=6124
182
188
  puts f.name
183
189
  end
184
190
  end
185
-
186
- # read from stream
187
- zip_data = open('foo.zip').raed
188
- stream = lambda { return !zip_data.slice(0, 256) }
191
+
192
+ # read from stream
193
+ zip_data = open('foo.zip') do |f|
194
+ f.binmode
195
+ f.read
196
+ end
197
+
198
+ stream = lambda { return zip_data.slice!(0, 256) }
189
199
 
190
200
  Zip::Archive.open_buffer(stream) do |ar|
191
201
  puts ar.num_files
@@ -198,7 +208,44 @@ https://rubyforge.org/frs/?group_id=6124
198
208
  end
199
209
  end
200
210
 
211
+ === adding directory recursively
212
+
213
+ require 'zipruby'
214
+
215
+ Zip::Archive.open('filename.zip', Zip::CREATE) do |ar|
216
+ ar.add_dir('dir')
217
+
218
+ Dir.glob('dir/**/*').each do |path|
219
+ if File.directory?(path)
220
+ ar.add_dir(path)
221
+ else
222
+ ar.add_file(path, path) # add_file(<entry name>, <source path>)
223
+ end
224
+ end
225
+ end
226
+
227
+ === extract all files
228
+
229
+ require 'zipruby'
230
+ require 'fileutils'
231
+
232
+ Zip::Archive.open('filename.zip') do |ar|
233
+ ar.each do |zf|
234
+ if zf.directory?
235
+ FileUtils.mkdir_p(zf.name)
236
+ else
237
+ dirname = File.dirname(zf.name)
238
+ FileUtils.mkdir_p(dirname) unless File.exist?(dirname)
239
+
240
+ open(zf.name, 'wb') do |f|
241
+ f << zf.read
242
+ end
243
+ end
244
+ end
245
+ end
246
+
201
247
  == License
248
+
202
249
  Copyright (c) 2008,2009 SUGAWARA Genki <sgwr_dts@yahoo.co.jp>
203
250
  All rights reserved.
204
251
 
@@ -226,6 +273,7 @@ https://rubyforge.org/frs/?group_id=6124
226
273
  DAMAGE.
227
274
 
228
275
  === libzip
276
+
229
277
  Zip/Ruby contains libzip.
230
278
 
231
279
  libzip is a C library for reading, creating, and modifying zip archives.
Binary file
data/zipruby.c CHANGED
@@ -198,6 +198,7 @@ void Init_zipruby() {
198
198
  #include "zipruby.h"
199
199
  #include "zipruby_archive.h"
200
200
  #include "zipruby_zip_source_proc.h"
201
+ #include "zipruby_zip_source_io.h"
201
202
  #include "tmpfile.h"
202
203
  #include "ruby.h"
203
204
  #ifndef RUBY_VM
@@ -218,7 +219,7 @@ static VALUE zipruby_archive_fopen(int argc, VALUE *argv, VALUE self);
218
219
  static VALUE zipruby_archive_get_stat(int argc, VALUE *argv, VALUE self);
219
220
  static VALUE zipruby_archive_add_buffer(VALUE self, VALUE name, VALUE source);
220
221
  static VALUE zipruby_archive_add_file(int argc, VALUE *argv, VALUE self);
221
- static VALUE zipruby_archive_add_io(VALUE self, VALUE name, VALUE io);
222
+ static VALUE zipruby_archive_add_io(int argc, VALUE *argv, VALUE self);
222
223
  static VALUE zipruby_archive_add_function(int argc, VALUE *argv, VALUE self);
223
224
  static VALUE zipruby_archive_replace_buffer(int argc, VALUE *argv, VALUE self);
224
225
  static VALUE zipruby_archive_replace_file(int argc, VALUE* argv, VALUE self);
@@ -246,6 +247,7 @@ static VALUE zipruby_archive_is_open(VALUE self);
246
247
  static VALUE zipruby_archive_decrypt(VALUE self, VALUE password);
247
248
  static VALUE zipruby_archive_encrypt(VALUE self, VALUE password);
248
249
  static VALUE zipruby_archive_read(VALUE self);
250
+ static VALUE zipruby_archive_add_dir(VALUE self, VALUE name);
249
251
 
250
252
  extern VALUE Zip;
251
253
  VALUE Archive;
@@ -268,7 +270,7 @@ void Init_zipruby_archive() {
268
270
  rb_define_method(Archive, "get_stat", zipruby_archive_get_stat, -1);
269
271
  rb_define_method(Archive, "add_buffer", zipruby_archive_add_buffer, 2);
270
272
  rb_define_method(Archive, "add_file", zipruby_archive_add_file, -1);
271
- rb_define_method(Archive, "add_io", zipruby_archive_add_io, 2);
273
+ rb_define_method(Archive, "add_io", zipruby_archive_add_io, -1);
272
274
  rb_define_method(Archive, "add", zipruby_archive_add_function, -1);
273
275
  rb_define_method(Archive, "replace_buffer", zipruby_archive_replace_buffer, -1);
274
276
  rb_define_method(Archive, "replace_file", zipruby_archive_replace_file, -1);
@@ -279,7 +281,7 @@ void Init_zipruby_archive() {
279
281
  rb_define_method(Archive, "add_or_replace_io", zipruby_archive_add_or_replace_io, -1);
280
282
  rb_define_method(Archive, "add_or_replace", zipruby_archive_add_or_replace_function, -1);
281
283
  rb_define_method(Archive, "update", zipruby_archive_update, -1);
282
- rb_define_method(Archive, "<<", zipruby_archive_add_io, 2);
284
+ rb_define_method(Archive, "<<", zipruby_archive_add_io, -1);
283
285
  rb_define_method(Archive, "get_comment", zipruby_archive_get_comment, -1);
284
286
  rb_define_method(Archive, "comment", zipruby_archive_get_comment, -1);
285
287
  rb_define_method(Archive, "comment=", zipruby_archive_set_comment, 1);
@@ -299,6 +301,7 @@ void Init_zipruby_archive() {
299
301
  rb_define_method(Archive, "decrypt", zipruby_archive_decrypt, 1);
300
302
  rb_define_method(Archive, "encrypt", zipruby_archive_encrypt, 1);
301
303
  rb_define_method(Archive, "read", zipruby_archive_read, 0);
304
+ rb_define_method(Archive, "add_dir", zipruby_archive_add_dir, 1);
302
305
  }
303
306
 
304
307
  static VALUE zipruby_archive_alloc(VALUE klass) {
@@ -835,28 +838,122 @@ static VALUE zipruby_archive_add_or_replace_file(int argc, VALUE *argv, VALUE se
835
838
  }
836
839
 
837
840
  /* */
838
- static VALUE zipruby_archive_add_io(VALUE self, VALUE name, VALUE io) {
839
- VALUE source;
841
+ static VALUE zipruby_archive_add_io(int argc, VALUE *argv, VALUE self) {
842
+ VALUE name, file, mtime;
843
+ struct zipruby_archive *p_archive;
844
+ struct zip_source *zsource;
845
+ struct read_io *z;
840
846
 
841
- Check_IO(io);
842
- source = rb_funcall(io, rb_intern("read"), 0);
847
+ rb_scan_args(argc, argv, "11", &name, &file);
848
+
849
+ if (NIL_P(file)) {
850
+ file = name;
851
+ name = Qnil;
852
+ }
853
+
854
+ Check_IO(file);
855
+
856
+ if (NIL_P(name)) {
857
+ if (rb_obj_is_kind_of(file, rb_cFile)) {
858
+ name = rb_funcall(rb_cFile, rb_intern("basename"), 1, rb_funcall(file, rb_intern("path"), 0));
859
+ } else {
860
+ rb_raise(rb_eRuntimeError, "Add io failed - %s: Entry name is not given", RSTRING(rb_inspect(file)));
861
+ }
862
+ }
863
+
864
+ if (rb_obj_is_kind_of(file, rb_cFile)) {
865
+ mtime = rb_funcall(file, rb_intern("mtime"), 0);
866
+ } else {
867
+ mtime = rb_funcall(rb_cTime, rb_intern("now"), 0);
868
+ }
869
+
870
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
871
+ Check_Archive(p_archive);
872
+
873
+ if ((z = malloc(sizeof(struct read_io))) == NULL) {
874
+ zip_unchange_all(p_archive->archive);
875
+ zip_unchange_archive(p_archive->archive);
876
+ rb_raise(rb_eRuntimeError, "Add io failed - %s: Cannot allocate memory", RSTRING(rb_inspect(file)));
877
+ }
878
+
879
+ z->io = file;
880
+ z->mtime = TIME2LONG(mtime);
881
+
882
+ if ((zsource = zip_source_io(p_archive->archive, z)) == NULL) {
883
+ free(z);
884
+ rb_raise(Error, "Add io failed - %s: %s", RSTRING(rb_inspect(file)), zip_strerror(p_archive->archive));
885
+ }
886
+
887
+ if (zip_add(p_archive->archive, RSTRING_PTR(name), zsource) == -1) {
888
+ zip_source_free(zsource);
889
+ zip_unchange_all(p_archive->archive);
890
+ zip_unchange_archive(p_archive->archive);
891
+ rb_raise(Error, "Add io failed - %s: %s", RSTRING_PTR(name), zip_strerror(p_archive->archive));
892
+ }
843
893
 
844
- return zipruby_archive_add_buffer(self, name, source);
894
+ return Qnil;
845
895
  }
846
896
 
847
897
  /* */
848
898
  static VALUE zipruby_archive_replace_io(int argc, VALUE *argv, VALUE self) {
849
- VALUE source, io, index, flags;
850
- VALUE _args[3];
899
+ VALUE file, index, flags, mtime;
900
+ struct zipruby_archive *p_archive;
901
+ struct zip_source *zsource;
902
+ struct read_io *z;
903
+ int i_index, i_flags = 0;
851
904
 
852
- rb_scan_args(argc, argv, "21", &index, &io, &flags);
853
- Check_IO(io);
854
- source = rb_funcall(io, rb_intern("read"), 0);
905
+ rb_scan_args(argc, argv, "21", &index, &file, &flags);
906
+
907
+ if (TYPE(index) != T_STRING && !FIXNUM_P(index)) {
908
+ rb_raise(rb_eTypeError, "wrong argument type %s (expected Fixnum or String)", rb_class2name(CLASS_OF(index)));
909
+ }
910
+
911
+ Check_IO(file);
912
+
913
+ if (!NIL_P(flags)) {
914
+ i_flags = NUM2INT(flags);
915
+ }
916
+
917
+ if (rb_obj_is_kind_of(file, rb_cFile)) {
918
+ mtime = rb_funcall(file, rb_intern("mtime"), 0);
919
+ } else {
920
+ mtime = rb_funcall(rb_cTime, rb_intern("now"), 0);
921
+ }
922
+
923
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
924
+ Check_Archive(p_archive);
925
+
926
+ if (FIXNUM_P(index)) {
927
+ i_index = NUM2INT(index);
928
+ } else if ((i_index = zip_name_locate(p_archive->archive, RSTRING_PTR(index), i_flags)) == -1) {
929
+ rb_raise(Error, "Replace io failed - %s: Archive does not contain a file", RSTRING_PTR(index));
930
+ }
931
+
932
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
933
+ Check_Archive(p_archive);
934
+
935
+ if ((z = malloc(sizeof(struct read_io))) == NULL) {
936
+ zip_unchange_all(p_archive->archive);
937
+ zip_unchange_archive(p_archive->archive);
938
+ rb_raise(rb_eRuntimeError, "Replace io failed at %d - %s: Cannot allocate memory", i_index, RSTRING(rb_inspect(file)));
939
+ }
940
+
941
+ z->io = file;
942
+ z->mtime = TIME2LONG(mtime);
855
943
 
856
- _args[0] = index;
857
- _args[1] = source;
858
- _args[2] = flags;
859
- return zipruby_archive_replace_buffer(2, _args, self);
944
+ if ((zsource = zip_source_io(p_archive->archive, z)) == NULL) {
945
+ free(z);
946
+ rb_raise(Error, "Replace io failed at %d - %s: %s", i_index, RSTRING(rb_inspect(file)), zip_strerror(p_archive->archive));
947
+ }
948
+
949
+ if (zip_replace(p_archive->archive, i_index, zsource) == -1) {
950
+ zip_source_free(zsource);
951
+ zip_unchange_all(p_archive->archive);
952
+ zip_unchange_archive(p_archive->archive);
953
+ rb_raise(Error, "Replace io failed at %d - %s: %s", i_index, RSTRING(rb_inspect(file)), zip_strerror(p_archive->archive));
954
+ }
955
+
956
+ return Qnil;
860
957
  }
861
958
 
862
959
  /* */
@@ -882,7 +979,8 @@ static VALUE zipruby_archive_add_or_replace_io(int argc, VALUE *argv, VALUE self
882
979
  VALUE _args[] = {INT2NUM(index), io, flags};
883
980
  return zipruby_archive_replace_io(2, _args, self);
884
981
  } else {
885
- return zipruby_archive_add_io(self, name, io);
982
+ VALUE _args[2] = { name, io };
983
+ return zipruby_archive_add_io(2, _args, self);
886
984
  }
887
985
  }
888
986
 
@@ -913,7 +1011,7 @@ static VALUE zipruby_archive_add_function(int argc, VALUE *argv, VALUE self) {
913
1011
  }
914
1012
 
915
1013
  z->proc = rb_block_proc();
916
- z->mtime = mtime;
1014
+ z->mtime = TIME2LONG(mtime);
917
1015
 
918
1016
  if ((zsource = zip_source_proc(p_archive->archive, z)) == NULL) {
919
1017
  free(z);
@@ -932,14 +1030,18 @@ static VALUE zipruby_archive_add_function(int argc, VALUE *argv, VALUE self) {
932
1030
 
933
1031
  /* */
934
1032
  static VALUE zipruby_archive_replace_function(int argc, VALUE *argv, VALUE self) {
935
- VALUE index, mtime;
1033
+ VALUE index, flags, mtime;
936
1034
  struct zipruby_archive *p_archive;
937
1035
  struct zip_source *zsource;
938
1036
  struct read_proc *z;
1037
+ int i_index, i_flags = 0;
939
1038
 
940
- rb_scan_args(argc, argv, "11", &index, &mtime);
1039
+ rb_scan_args(argc, argv, "12", &index, &mtime, &flags);
941
1040
  rb_need_block();
942
- Check_Type(index, T_FIXNUM);
1041
+
1042
+ if (TYPE(index) != T_STRING && !FIXNUM_P(index)) {
1043
+ rb_raise(rb_eTypeError, "wrong argument type %s (expected Fixnum or String)", rb_class2name(CLASS_OF(index)));
1044
+ }
943
1045
 
944
1046
  if (NIL_P(mtime)) {
945
1047
  mtime = rb_funcall(rb_cTime, rb_intern("now"), 0);
@@ -947,28 +1049,38 @@ static VALUE zipruby_archive_replace_function(int argc, VALUE *argv, VALUE self)
947
1049
  rb_raise(rb_eTypeError, "wrong argument type %s (expected Time)", rb_class2name(CLASS_OF(mtime)));
948
1050
  }
949
1051
 
1052
+ if (!NIL_P(flags)) {
1053
+ i_flags = NUM2INT(flags);
1054
+ }
1055
+
950
1056
  Data_Get_Struct(self, struct zipruby_archive, p_archive);
951
1057
  Check_Archive(p_archive);
952
1058
 
1059
+ if (FIXNUM_P(index)) {
1060
+ i_index = NUM2INT(index);
1061
+ } else if ((i_index = zip_name_locate(p_archive->archive, RSTRING_PTR(index), i_flags)) == -1) {
1062
+ rb_raise(Error, "Replace file failed - %s: Archive does not contain a file", RSTRING_PTR(index));
1063
+ }
1064
+
953
1065
  if ((z = malloc(sizeof(struct read_proc))) == NULL) {
954
1066
  zip_unchange_all(p_archive->archive);
955
1067
  zip_unchange_archive(p_archive->archive);
956
- rb_raise(rb_eRuntimeError, "Replace failed at %d: Cannot allocate memory", NUM2INT(index));
1068
+ rb_raise(rb_eRuntimeError, "Replace failed at %d: Cannot allocate memory", i_index);
957
1069
  }
958
1070
 
959
1071
  z->proc = rb_block_proc();
960
- z->mtime = mtime;
1072
+ z->mtime = TIME2LONG(mtime);
961
1073
 
962
1074
  if ((zsource = zip_source_proc(p_archive->archive, z)) == NULL) {
963
1075
  free(z);
964
- rb_raise(Error, "Replace failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
1076
+ rb_raise(Error, "Replace failed at %d: %s", i_index, zip_strerror(p_archive->archive));
965
1077
  }
966
1078
 
967
- if (zip_replace(p_archive->archive, NUM2INT(index), zsource) == -1) {
1079
+ if (zip_replace(p_archive->archive, i_index, zsource) == -1) {
968
1080
  zip_source_free(zsource);
969
1081
  zip_unchange_all(p_archive->archive);
970
1082
  zip_unchange_archive(p_archive->archive);
971
- rb_raise(Error, "Replace failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
1083
+ rb_raise(Error, "Replace failed at %d: %s", i_index, zip_strerror(p_archive->archive));
972
1084
  }
973
1085
 
974
1086
  return Qnil;
@@ -1522,6 +1634,23 @@ static VALUE zipruby_archive_read(VALUE self) {
1522
1634
 
1523
1635
  return retval;
1524
1636
  }
1637
+
1638
+ /* */
1639
+ static VALUE zipruby_archive_add_dir(VALUE self, VALUE name) {
1640
+ struct zipruby_archive *p_archive;
1641
+
1642
+ Check_Type(name, T_STRING);
1643
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
1644
+ Check_Archive(p_archive);
1645
+
1646
+ if (zip_add_dir(p_archive->archive, RSTRING_PTR(name)) == -1) {
1647
+ zip_unchange_all(p_archive->archive);
1648
+ zip_unchange_archive(p_archive->archive);
1649
+ rb_raise(Error, "Add dir failed - %s: %s", RSTRING_PTR(name), zip_strerror(p_archive->archive));
1650
+ }
1651
+
1652
+ return Qnil;
1653
+ }
1525
1654
  #include "zipruby.h"
1526
1655
  #include "zipruby_error.h"
1527
1656
  #include "ruby.h"
@@ -1565,6 +1694,7 @@ static VALUE zipruby_file_mtime(VALUE self);
1565
1694
  static VALUE zipruby_file_comp_size(VALUE self);
1566
1695
  static VALUE zipruby_file_comp_method(VALUE self);
1567
1696
  static VALUE zipruby_file_encryption_method(VALUE self);
1697
+ static VALUE zipruby_file_is_directory(VALUE self);
1568
1698
 
1569
1699
  extern VALUE Zip;
1570
1700
  extern VALUE Archive;
@@ -1594,6 +1724,7 @@ void Init_zipruby_file() {
1594
1724
  rb_define_method(File, "comp_size", zipruby_file_comp_size, 0);
1595
1725
  rb_define_method(File, "comp_method", zipruby_file_comp_method, 0);
1596
1726
  rb_define_method(File, "encryption_method", zipruby_file_encryption_method, 0);
1727
+ rb_define_method(File, "directory?", zipruby_file_is_directory, 0);
1597
1728
  }
1598
1729
 
1599
1730
  static VALUE zipruby_file_alloc(VALUE klass) {
@@ -1858,7 +1989,7 @@ static VALUE zipruby_file_name(VALUE self) {
1858
1989
  Data_Get_Struct(self, struct zipruby_file, p_file);
1859
1990
  Check_File(p_file);
1860
1991
 
1861
- return rb_funcall(p_file->v_sb, rb_intern("name"), 0);
1992
+ return zipruby_stat_name(p_file->v_sb);
1862
1993
  }
1863
1994
 
1864
1995
  /* */
@@ -1868,7 +1999,7 @@ static VALUE zipruby_file_index(VALUE self) {
1868
1999
  Data_Get_Struct(self, struct zipruby_file, p_file);
1869
2000
  Check_File(p_file);
1870
2001
 
1871
- return rb_funcall(p_file->v_sb, rb_intern("index"), 0);
2002
+ return zipruby_stat_index(p_file->v_sb);
1872
2003
  }
1873
2004
 
1874
2005
  /* */
@@ -1878,7 +2009,7 @@ static VALUE zipruby_file_crc(VALUE self) {
1878
2009
  Data_Get_Struct(self, struct zipruby_file, p_file);
1879
2010
  Check_File(p_file);
1880
2011
 
1881
- return rb_funcall(p_file->v_sb, rb_intern("crc"), 0);
2012
+ return zipruby_stat_crc(p_file->v_sb);
1882
2013
  }
1883
2014
 
1884
2015
  /* */
@@ -1888,7 +2019,7 @@ static VALUE zipruby_file_size(VALUE self) {
1888
2019
  Data_Get_Struct(self, struct zipruby_file, p_file);
1889
2020
  Check_File(p_file);
1890
2021
 
1891
- return rb_funcall(p_file->v_sb, rb_intern("size"), 0);
2022
+ return zipruby_stat_size(p_file->v_sb);
1892
2023
  }
1893
2024
 
1894
2025
  /* */
@@ -1898,7 +2029,7 @@ static VALUE zipruby_file_mtime(VALUE self) {
1898
2029
  Data_Get_Struct(self, struct zipruby_file, p_file);
1899
2030
  Check_File(p_file);
1900
2031
 
1901
- return rb_funcall(p_file->v_sb, rb_intern("mtime"), 0);
2032
+ return zipruby_stat_mtime(p_file->v_sb);
1902
2033
  }
1903
2034
 
1904
2035
  /* */
@@ -1908,7 +2039,7 @@ static VALUE zipruby_file_comp_size(VALUE self) {
1908
2039
  Data_Get_Struct(self, struct zipruby_file, p_file);
1909
2040
  Check_File(p_file);
1910
2041
 
1911
- return rb_funcall(p_file->v_sb, rb_intern("comp_size"), 0);
2042
+ return zipruby_stat_comp_size(p_file->v_sb);
1912
2043
  }
1913
2044
 
1914
2045
  /* */
@@ -1918,7 +2049,7 @@ static VALUE zipruby_file_comp_method(VALUE self) {
1918
2049
  Data_Get_Struct(self, struct zipruby_file, p_file);
1919
2050
  Check_File(p_file);
1920
2051
 
1921
- return rb_funcall(p_file->v_sb, rb_intern("comp_method"), 0);
2052
+ return zipruby_stat_comp_method(p_file->v_sb);
1922
2053
  }
1923
2054
 
1924
2055
  /* */
@@ -1928,8 +2059,20 @@ static VALUE zipruby_file_encryption_method(VALUE self) {
1928
2059
  Data_Get_Struct(self, struct zipruby_file, p_file);
1929
2060
  Check_File(p_file);
1930
2061
 
1931
- return rb_funcall(p_file->v_sb, rb_intern("encryption_method"), 0);
2062
+ return zipruby_stat_encryption_method(p_file->v_sb);
1932
2063
  }
2064
+
2065
+ /* */
2066
+ static VALUE zipruby_file_is_directory(VALUE self) {
2067
+ struct zipruby_file *p_file;
2068
+
2069
+ Data_Get_Struct(self, struct zipruby_file, p_file);
2070
+ Check_File(p_file);
2071
+
2072
+ return zipruby_stat_is_directory(p_file->v_sb);
2073
+ }
2074
+ #include <string.h>
2075
+
1933
2076
  #include "zip.h"
1934
2077
  #include "zipruby.h"
1935
2078
  #include "zipruby_archive.h"
@@ -1939,14 +2082,6 @@ static VALUE zipruby_file_encryption_method(VALUE self) {
1939
2082
  static VALUE zipruby_stat_alloc(VALUE klass);
1940
2083
  static void zipruby_stat_free(struct zipruby_stat *p);
1941
2084
  static VALUE zipruby_stat_initialize(int argc, VALUE *argv, VALUE self);
1942
- static VALUE zipruby_stat_name(VALUE self);
1943
- static VALUE zipruby_stat_index(VALUE self);
1944
- static VALUE zipruby_stat_crc(VALUE self);
1945
- static VALUE zipruby_stat_size(VALUE self);
1946
- static VALUE zipruby_stat_mtime(VALUE self);
1947
- static VALUE zipruby_stat_comp_size(VALUE self);
1948
- static VALUE zipruby_stat_comp_method(VALUE self);
1949
- static VALUE zipruby_stat_encryption_method(VALUE self);
1950
2085
 
1951
2086
  extern VALUE Zip;
1952
2087
  extern VALUE Archive;
@@ -1965,6 +2100,7 @@ void Init_zipruby_stat() {
1965
2100
  rb_define_method(Stat, "comp_size", zipruby_stat_comp_size, 0);
1966
2101
  rb_define_method(Stat, "comp_method", zipruby_stat_comp_method, 0);
1967
2102
  rb_define_method(Stat, "encryption_method", zipruby_stat_encryption_method, 0);
2103
+ rb_define_method(Stat, "directory?", zipruby_stat_is_directory, 0);
1968
2104
  }
1969
2105
 
1970
2106
  static VALUE zipruby_stat_alloc(VALUE klass) {
@@ -2024,7 +2160,7 @@ static VALUE zipruby_stat_initialize(int argc, VALUE *argv, VALUE self) {
2024
2160
  }
2025
2161
 
2026
2162
  /* */
2027
- static VALUE zipruby_stat_name(VALUE self) {
2163
+ VALUE zipruby_stat_name(VALUE self) {
2028
2164
  struct zipruby_stat *p_stat;
2029
2165
 
2030
2166
  Data_Get_Struct(self, struct zipruby_stat, p_stat);
@@ -2033,7 +2169,7 @@ static VALUE zipruby_stat_name(VALUE self) {
2033
2169
  }
2034
2170
 
2035
2171
  /* */
2036
- static VALUE zipruby_stat_index(VALUE self) {
2172
+ VALUE zipruby_stat_index(VALUE self) {
2037
2173
  struct zipruby_stat *p_stat;
2038
2174
 
2039
2175
  Data_Get_Struct(self, struct zipruby_stat, p_stat);
@@ -2042,7 +2178,7 @@ static VALUE zipruby_stat_index(VALUE self) {
2042
2178
  }
2043
2179
 
2044
2180
  /* */
2045
- static VALUE zipruby_stat_crc(VALUE self) {
2181
+ VALUE zipruby_stat_crc(VALUE self) {
2046
2182
  struct zipruby_stat *p_stat;
2047
2183
 
2048
2184
  Data_Get_Struct(self, struct zipruby_stat, p_stat);
@@ -2051,7 +2187,7 @@ static VALUE zipruby_stat_crc(VALUE self) {
2051
2187
  }
2052
2188
 
2053
2189
  /* */
2054
- static VALUE zipruby_stat_size(VALUE self) {
2190
+ VALUE zipruby_stat_size(VALUE self) {
2055
2191
  struct zipruby_stat *p_stat;
2056
2192
 
2057
2193
  Data_Get_Struct(self, struct zipruby_stat, p_stat);
@@ -2060,7 +2196,7 @@ static VALUE zipruby_stat_size(VALUE self) {
2060
2196
  }
2061
2197
 
2062
2198
  /* */
2063
- static VALUE zipruby_stat_mtime(VALUE self) {
2199
+ VALUE zipruby_stat_mtime(VALUE self) {
2064
2200
  struct zipruby_stat *p_stat;
2065
2201
 
2066
2202
  Data_Get_Struct(self, struct zipruby_stat, p_stat);
@@ -2069,7 +2205,7 @@ static VALUE zipruby_stat_mtime(VALUE self) {
2069
2205
  }
2070
2206
 
2071
2207
  /* */
2072
- static VALUE zipruby_stat_comp_size(VALUE self) {
2208
+ VALUE zipruby_stat_comp_size(VALUE self) {
2073
2209
  struct zipruby_stat *p_stat;
2074
2210
 
2075
2211
  Data_Get_Struct(self, struct zipruby_stat, p_stat);
@@ -2078,7 +2214,7 @@ static VALUE zipruby_stat_comp_size(VALUE self) {
2078
2214
  }
2079
2215
 
2080
2216
  /* */
2081
- static VALUE zipruby_stat_comp_method(VALUE self) {
2217
+ VALUE zipruby_stat_comp_method(VALUE self) {
2082
2218
  struct zipruby_stat *p_stat;
2083
2219
 
2084
2220
  Data_Get_Struct(self, struct zipruby_stat, p_stat);
@@ -2087,13 +2223,37 @@ static VALUE zipruby_stat_comp_method(VALUE self) {
2087
2223
  }
2088
2224
 
2089
2225
  /* */
2090
- static VALUE zipruby_stat_encryption_method(VALUE self) {
2226
+ VALUE zipruby_stat_encryption_method(VALUE self) {
2091
2227
  struct zipruby_stat *p_stat;
2092
2228
 
2093
2229
  Data_Get_Struct(self, struct zipruby_stat, p_stat);
2094
2230
 
2095
2231
  return INT2NUM(p_stat->sb->encryption_method);
2096
2232
  }
2233
+
2234
+ /* */
2235
+ VALUE zipruby_stat_is_directory(VALUE self) {
2236
+ struct zipruby_stat *p_stat;
2237
+ const char *name;
2238
+ size_t name_len;
2239
+ off_t size;
2240
+
2241
+ Data_Get_Struct(self, struct zipruby_stat, p_stat);
2242
+ name = p_stat->sb->name;
2243
+ size = p_stat->sb->size;
2244
+
2245
+ if (!name || size != 0) {
2246
+ return Qfalse;
2247
+ }
2248
+
2249
+ name_len = strlen(name);
2250
+
2251
+ if (name_len > 0 && name[name_len - 1] == '/') {
2252
+ return Qtrue;
2253
+ } else {
2254
+ return Qfalse;
2255
+ }
2256
+ }
2097
2257
  #include "ruby.h"
2098
2258
  #include "zip.h"
2099
2259
  #include "zipruby.h"
@@ -2134,6 +2294,91 @@ void Init_zipruby_zip() {
2134
2294
  }
2135
2295
  #include <string.h>
2136
2296
 
2297
+ #include "zip.h"
2298
+ #include "zipint.h"
2299
+ #include "zipruby_zip_source_io.h"
2300
+ #include "ruby.h"
2301
+
2302
+ #define IO_READ_BUFSIZE 8192
2303
+
2304
+ static VALUE io_read(VALUE io) {
2305
+ return rb_funcall(io, rb_intern("read"), 1, INT2FIX(IO_READ_BUFSIZE));
2306
+ }
2307
+
2308
+ static ssize_t read_io(void *state, void *data, size_t len, enum zip_source_cmd cmd) {
2309
+ struct read_io *z;
2310
+ VALUE src;
2311
+ char *buf;
2312
+ size_t n;
2313
+ int status = 0;
2314
+
2315
+ z = (struct read_io *) state;
2316
+ buf = (char *) data;
2317
+
2318
+ switch (cmd) {
2319
+ case ZIP_SOURCE_OPEN:
2320
+ return 0;
2321
+
2322
+ case ZIP_SOURCE_READ:
2323
+ src = rb_protect(io_read, z->io, NULL);
2324
+
2325
+ if (status != 0) {
2326
+ VALUE message, clazz;
2327
+
2328
+ #if defined(RUBY_VM)
2329
+ message = rb_funcall(rb_errinfo(), rb_intern("message"), 0);
2330
+ clazz = CLASS_OF(rb_errinfo());
2331
+ #else
2332
+ message = rb_funcall(ruby_errinfo, rb_intern("message"), 0);
2333
+ clazz = CLASS_OF(ruby_errinfo);
2334
+ #endif
2335
+
2336
+ rb_warn("Error in IO: %s (%s)", RSTRING_PTR(message), rb_class2name(clazz));
2337
+ return -1;
2338
+ }
2339
+
2340
+ if (TYPE(src) != T_STRING) {
2341
+ return 0;
2342
+ }
2343
+
2344
+ n = RSTRING_LEN(src);
2345
+
2346
+ if (n > 0) {
2347
+ n = (n > len) ? len : n;
2348
+ memcpy(buf, RSTRING_PTR(src), n);
2349
+ }
2350
+
2351
+ return n;
2352
+
2353
+ case ZIP_SOURCE_CLOSE:
2354
+ return 0;
2355
+
2356
+ case ZIP_SOURCE_STAT:
2357
+ {
2358
+ struct zip_stat *st = (struct zip_stat *)data;
2359
+ zip_stat_init(st);
2360
+ st->mtime = z->mtime;
2361
+ return sizeof(*st);
2362
+ }
2363
+
2364
+ case ZIP_SOURCE_ERROR:
2365
+ return 0;
2366
+
2367
+ case ZIP_SOURCE_FREE:
2368
+ free(z);
2369
+ return 0;
2370
+ }
2371
+
2372
+ return -1;
2373
+ }
2374
+
2375
+ struct zip_source *zip_source_io(struct zip *za, struct read_io *z) {
2376
+ struct zip_source *zs;
2377
+ zs = zip_source_function(za, read_io, z);
2378
+ return zs;
2379
+ }
2380
+ #include <string.h>
2381
+
2137
2382
  #include "zip.h"
2138
2383
  #include "zipint.h"
2139
2384
  #include "zipruby_zip_source_proc.h"
@@ -2148,6 +2393,7 @@ static ssize_t read_proc(void *state, void *data, size_t len, enum zip_source_cm
2148
2393
  VALUE src;
2149
2394
  char *buf;
2150
2395
  size_t n;
2396
+ int status = 0;
2151
2397
 
2152
2398
  z = (struct read_proc *) state;
2153
2399
  buf = (char *) data;
@@ -2157,7 +2403,22 @@ static ssize_t read_proc(void *state, void *data, size_t len, enum zip_source_cm
2157
2403
  return 0;
2158
2404
 
2159
2405
  case ZIP_SOURCE_READ:
2160
- src = rb_protect(proc_call, z->proc, NULL);
2406
+ src = rb_protect(proc_call, z->proc, &status);
2407
+
2408
+ if (status != 0) {
2409
+ VALUE message, clazz;
2410
+
2411
+ #if defined(RUBY_VM)
2412
+ message = rb_funcall(rb_errinfo(), rb_intern("message"), 0);
2413
+ clazz = CLASS_OF(rb_errinfo());
2414
+ #else
2415
+ message = rb_funcall(ruby_errinfo, rb_intern("message"), 0);
2416
+ clazz = CLASS_OF(ruby_errinfo);
2417
+ #endif
2418
+
2419
+ rb_warn("Error in Proc: %s (%s)", RSTRING_PTR(message), rb_class2name(clazz));
2420
+ return -1;
2421
+ }
2161
2422
 
2162
2423
  if (TYPE(src) != T_STRING) {
2163
2424
  return 0;
@@ -2179,7 +2440,7 @@ static ssize_t read_proc(void *state, void *data, size_t len, enum zip_source_cm
2179
2440
  {
2180
2441
  struct zip_stat *st = (struct zip_stat *)data;
2181
2442
  zip_stat_init(st);
2182
- st->mtime = NUM2LONG(rb_funcall(z->mtime, rb_intern("tv_sec"), 0));
2443
+ st->mtime = z->mtime;
2183
2444
  return sizeof(*st);
2184
2445
  }
2185
2446
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zipruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: mswin32
6
6
  authors:
7
7
  - winebarrel
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-19 00:00:00 +09:00
12
+ date: 2009-05-16 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -23,11 +23,13 @@ extra_rdoc_files:
23
23
  - README.txt
24
24
  - zipruby.c
25
25
  - LICENSE.libzip
26
+ - ChangeLog
26
27
  files:
27
28
  - lib/i386-mswin32/zipruby.so
28
29
  - README.txt
29
30
  - zipruby.c
30
31
  - LICENSE.libzip
32
+ - ChangeLog
31
33
  has_rdoc: true
32
34
  homepage: http://zipruby.rubyforge.org
33
35
  post_install_message: