zipruby 0.1.2-mswin32 → 0.2.0-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 (4) hide show
  1. data/README.txt +48 -2
  2. data/lib/i386-mswin32/zipruby.so +0 -0
  3. data/zipruby.c +67 -11
  4. metadata +2 -2
data/README.txt CHANGED
@@ -43,7 +43,24 @@ https://rubyforge.org/frs/?group_id=6124
43
43
 
44
44
  # Zip::Archive includes Enumerable
45
45
  entry_names = ar.map do |f|
46
- f.naem
46
+ f.name
47
+ end
48
+ end
49
+
50
+ # read huge entry
51
+ BUFSIZE = 8192
52
+
53
+ Zip::Archive.open('filename.zip') do |ar|
54
+ ar.each do |f|
55
+ buf = ''
56
+
57
+ while chunk = f.read(BUFSIZE)
58
+ buf << chunk
59
+ end
60
+ # or
61
+ # f.read do |chunk|
62
+ # buf << chunk
63
+ # end
47
64
  end
48
65
  end
49
66
 
@@ -62,6 +79,20 @@ https://rubyforge.org/frs/?group_id=6124
62
79
  # add file to zip archive from buffer
63
80
  ar.add_buffer('zoo.txt', 'Hello, world!')
64
81
  end
82
+
83
+ # include directory in zip archive
84
+ Zip::Archive.open('filename.zip') do |ar|
85
+ ar.add_file('dirname/foo.txt', 'foo.txt')
86
+ # args: <entry name>, <source>
87
+
88
+ open('bar.txt') do |f|
89
+ ar.add_filep('dirname/bar.txt', f)
90
+ # args: <entry name>, <source>
91
+ end
92
+
93
+ ar.add_buffer('dirname/zoo.txt', 'Hello, world!')
94
+ # args: <entry name>, <source>
95
+ end
65
96
 
66
97
  === modifying zip archives
67
98
 
@@ -83,13 +114,28 @@ https://rubyforge.org/frs/?group_id=6124
83
114
  ar.add_or_replace_file(3, 'foo.txt')
84
115
  end
85
116
 
86
- # ar1 imports ar2 entries
117
+ # append comment
118
+ Zip::Archive.open('filename.zip') do |ar|
119
+ ar.comment = <<-EOS
120
+ jugem jugem gokou no surikere
121
+ kaijari suigyo no
122
+ suigyoumatsu unraimatsu furaimatsu
123
+ EOS
124
+ end
125
+
126
+ # ar1 import ar2 entries
87
127
  Zip::Archive.open('ar1.zip') do |ar1|
88
128
  Zip::Archive.open('ar2.zip') do |ar2|
89
129
  ar1.update(ar2)
90
130
  end
91
131
  end
92
132
 
133
+ === decrypt zip archives
134
+
135
+ require 'zipruby'
136
+
137
+ Zip::Archive.decrypt('filename.zip', 'password')
138
+
93
139
  == License
94
140
  Copyright (c) 2008 SUGAWARA Genki <sgwr_dts@yahoo.co.jp>
95
141
  All rights reserved.
Binary file
data/zipruby.c CHANGED
@@ -28,6 +28,7 @@ void Init_zipruby() {
28
28
  static VALUE zipruby_archive_alloc(VALUE klass);
29
29
  static void zipruby_archive_free(struct zipruby_archive *p);
30
30
  static VALUE zipruby_archive_s_open(int argc, VALUE *argv, VALUE self);
31
+ static VALUE zipruby_archive_s_decrypt(VALUE self, VALUE path, VALUE password);
31
32
  static VALUE zipruby_archive_close(VALUE self);
32
33
  static VALUE zipruby_archive_num_files(VALUE self);
33
34
  static VALUE zipruby_archive_get_name(int argc, VALUE *argv, VALUE self);
@@ -67,6 +68,7 @@ void Init_zipruby_archive() {
67
68
  rb_define_alloc_func(Archive, zipruby_archive_alloc);
68
69
  rb_include_module(Archive, rb_mEnumerable);
69
70
  rb_define_singleton_method(Archive, "open", zipruby_archive_s_open, -1);
71
+ rb_define_singleton_method(Archive, "decrypt", zipruby_archive_s_decrypt, 2);
70
72
  rb_define_method(Archive, "close", zipruby_archive_close, 0);
71
73
  rb_define_method(Archive, "num_files", zipruby_archive_num_files, 0);
72
74
  rb_define_method(Archive, "get_name", zipruby_archive_get_name, -1);
@@ -150,7 +152,27 @@ static VALUE zipruby_archive_s_open(int argc, VALUE *argv, VALUE self) {
150
152
  } else {
151
153
  return archive;
152
154
  }
153
- };
155
+ }
156
+
157
+ /* */
158
+ static VALUE zipruby_archive_s_decrypt(VALUE self, VALUE path, VALUE password) {
159
+ int errorp, wrongpwd;
160
+
161
+ Check_Type(path, T_STRING);
162
+ Check_Type(password, T_STRING);
163
+
164
+ if (zip_decrypt(StringValuePtr(path), StringValuePtr(password), RSTRING(password)->len, &errorp, &wrongpwd) == -1) {
165
+ if (wrongpwd) {
166
+ rb_raise(Error, "Decrypt archive failed - %s: Wrong password", StringValuePtr(path));
167
+ } else {
168
+ char errstr[ERRSTR_BUFSIZE];
169
+ zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
170
+ rb_raise(Error, "Decrypt archive failed - %s: %s", StringValuePtr(path), errstr);
171
+ }
172
+ }
173
+
174
+ return Qnil;
175
+ }
154
176
 
155
177
  /* */
156
178
  static VALUE zipruby_archive_close(VALUE self) {
@@ -288,7 +310,11 @@ static VALUE zipruby_archive_replace_buffer(VALUE self, VALUE index, VALUE sourc
288
310
  Check_Archive(p_archive);
289
311
 
290
312
  len = RSTRING(source)->len;
291
- data = malloc(len);
313
+
314
+ if ((data = malloc(len)) == NULL) {
315
+ rb_raise(rb_eRuntimeError, "Replace file failed: Cannot allocate memory");
316
+ }
317
+
292
318
  memcpy(data, StringValuePtr(source), len);
293
319
 
294
320
  if ((zsource = zip_source_buffer(p_archive->archive, data, len, 1)) == NULL) {
@@ -825,13 +851,15 @@ void Init_zipruby_error() {
825
851
  #include "zipruby_stat.h"
826
852
  #include "ruby.h"
827
853
 
854
+ #define MIN(a, b) ((a) < (b) ? (a) : (b))
855
+
828
856
  static VALUE zipruby_file(VALUE klass);
829
857
  static VALUE zipruby_file_alloc(VALUE klass);
830
858
  static void zipruby_file_mark(struct zipruby_file *p);
831
859
  static void zipruby_file_free(struct zipruby_file *p);
832
860
  static VALUE zipruby_file_initialize(int argc, VALUE *argv, VALUE self);
833
861
  static VALUE zipruby_file_close(VALUE self);
834
- static VALUE zipruby_file_read(VALUE self);
862
+ static VALUE zipruby_file_read(int argc, VALUE *argv, VALUE self);
835
863
  static VALUE zipruby_file_stat(VALUE self);
836
864
  static VALUE zipruby_file_get_comment(int argc, VALUE *argv, VALUE self);
837
865
  static VALUE zipruby_file_set_comment(VALUE self, VALUE comment);
@@ -858,7 +886,7 @@ void Init_zipruby_file() {
858
886
  rb_define_alloc_func(File, zipruby_file_alloc);
859
887
  rb_define_method(File, "initialize", zipruby_file_initialize, -1);
860
888
  rb_define_method(File, "close", zipruby_file_close, 0);
861
- rb_define_method(File, "read", zipruby_file_read, 0);
889
+ rb_define_method(File, "read", zipruby_file_read, -1);
862
890
  rb_define_method(File, "stat", zipruby_file_stat, 0);
863
891
  rb_define_method(File, "get_comment", zipruby_file_get_comment, -1);
864
892
  rb_define_method(File, "comment", zipruby_file_get_comment, -1);
@@ -974,27 +1002,55 @@ static VALUE zipruby_file_close(VALUE self) {
974
1002
  }
975
1003
 
976
1004
  /* */
977
- static VALUE zipruby_file_read(VALUE self) {
1005
+ static VALUE zipruby_file_read(int argc, VALUE *argv, VALUE self) {
1006
+ VALUE size, retval = Qnil;
978
1007
  struct zipruby_file *p_file;
979
1008
  struct zip_stat sb;
980
- char *buf;
1009
+ int block_given;
1010
+ size_t bytes_left;
1011
+ char buf[DATA_BUFSIZE];
1012
+ ssize_t n;
981
1013
 
1014
+ rb_scan_args(argc, argv, "01", &size);
982
1015
  Data_Get_Struct(self, struct zipruby_file, p_file);
983
1016
  Check_File(p_file);
984
-
985
1017
  zip_stat_init(&sb);
986
1018
 
987
1019
  if (zip_stat_index(p_file->archive, p_file->sb->index, 0, &sb)) {
988
1020
  rb_raise(Error, "Read file failed: %s", zip_strerror(p_file->archive));
989
1021
  }
990
1022
 
991
- buf = alloca(sb.size);
992
-
993
- if (zip_fread(p_file->file, buf, sb.size) == -1) {
1023
+ if (NIL_P(size)) {
1024
+ bytes_left = sb.size;
1025
+ } else {
1026
+ bytes_left = NUM2LONG(size);
1027
+ }
1028
+
1029
+ if (bytes_left <= 0) {
1030
+ return Qnil;
1031
+ }
1032
+
1033
+ block_given = rb_block_given_p();
1034
+
1035
+ while ((n = zip_fread(p_file->file, buf, MIN(bytes_left, sizeof(buf)))) > 0) {
1036
+ if (block_given) {
1037
+ rb_yield(rb_str_new(buf, n));
1038
+ } else {
1039
+ if (NIL_P(retval)) {
1040
+ retval = rb_str_new(buf, n);
1041
+ } else {
1042
+ rb_str_buf_cat(retval, buf, n);
1043
+ }
1044
+ }
1045
+
1046
+ bytes_left -= n;
1047
+ }
1048
+
1049
+ if (n == -1) {
994
1050
  rb_raise(Error, "Read file failed: %s", zip_file_strerror(p_file->file));
995
1051
  }
996
1052
 
997
- return rb_str_new(buf, sb.size);
1053
+ return retval;
998
1054
  }
999
1055
 
1000
1056
  /* */
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.1.2
4
+ version: 0.2.0
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: 2008-04-26 00:00:00 +09:00
12
+ date: 2008-04-30 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies: []
15
15