zipruby 0.2.3-mswin32 → 0.2.4-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 +10 -0
  2. data/lib/i386-mswin32/zipruby.so +0 -0
  3. data/zipruby.c +179 -0
  4. metadata +1 -1
data/README.txt CHANGED
@@ -93,6 +93,16 @@ https://rubyforge.org/frs/?group_id=6124
93
93
  ar.add_buffer('dirname/zoo.txt', 'Hello, world!')
94
94
  # args: <entry name>, <source>
95
95
  end
96
+
97
+ # add huge file
98
+ source = %w(London Bridge is falling down)
99
+
100
+ Zip::Archive.open('filename.zip') do |ar|
101
+ # lb.txt => 'LondonBridgeisfallingdown'
102
+ ar.add('lb.txt') do # add(<filename>, <mtime>)
103
+ source.shift # end of stream is nil
104
+ end
105
+ end
96
106
 
97
107
  === modifying zip archive
98
108
 
Binary file
data/zipruby.c CHANGED
@@ -22,6 +22,7 @@ void Init_zipruby() {
22
22
  #include "zip.h"
23
23
  #include "zipruby.h"
24
24
  #include "zipruby_archive.h"
25
+ #include "zipruby_zip_source_proc.h"
25
26
  #include "ruby.h"
26
27
  #include "rubyio.h"
27
28
 
@@ -38,12 +39,15 @@ static VALUE zipruby_archive_get_stat(int argc, VALUE *argv, VALUE self);
38
39
  static VALUE zipruby_archive_add_buffer(VALUE self, VALUE name, VALUE source);
39
40
  static VALUE zipruby_archive_add_file(int argc, VALUE *argv, VALUE self);
40
41
  static VALUE zipruby_archive_add_filep(int argc, VALUE *argv, VALUE self);
42
+ static VALUE zipruby_archive_add_function(int argc, VALUE *argv, VALUE self);
41
43
  static VALUE zipruby_archive_replace_buffer(VALUE self, VALUE index, VALUE source);
42
44
  static VALUE zipruby_archive_replace_file(VALUE self, VALUE index, VALUE fname);
43
45
  static VALUE zipruby_archive_replace_filep(VALUE self, VALUE index, VALUE file);
46
+ static VALUE zipruby_archive_replace_function(int argc, VALUE *argv, VALUE self);
44
47
  static VALUE zipruby_archive_add_or_replace_buffer(VALUE self, VALUE name, VALUE source);
45
48
  static VALUE zipruby_archive_add_or_replace_file(int argc, VALUE *argv, VALUE self);
46
49
  static VALUE zipruby_archive_add_or_replace_filep(int argc, VALUE *argv, VALUE self);
50
+ static VALUE zipruby_archive_add_or_replace_function(int argc, VALUE *argv, VALUE self);
47
51
  static VALUE zipruby_archive_update(VALUE self, VALUE srcarchive);
48
52
  static VALUE zipruby_archive_get_comment(int argc, VALUE *argv, VALUE self);
49
53
  static VALUE zipruby_archive_set_comment(VALUE self, VALUE comment);
@@ -79,12 +83,15 @@ void Init_zipruby_archive() {
79
83
  rb_define_method(Archive, "add_buffer", zipruby_archive_add_buffer, 2);
80
84
  rb_define_method(Archive, "add_file", zipruby_archive_add_file, -1);
81
85
  rb_define_method(Archive, "add_filep", zipruby_archive_add_filep, -1);
86
+ rb_define_method(Archive, "add", zipruby_archive_add_function, -1);
82
87
  rb_define_method(Archive, "replace_buffer", zipruby_archive_replace_buffer, 2);
83
88
  rb_define_method(Archive, "replace_file", zipruby_archive_replace_file, 2);
84
89
  rb_define_method(Archive, "replace_filep", zipruby_archive_replace_filep, 2);
90
+ rb_define_method(Archive, "replace", zipruby_archive_replace_function, -1);
85
91
  rb_define_method(Archive, "add_or_replace_buffer", zipruby_archive_add_or_replace_buffer, 2);
86
92
  rb_define_method(Archive, "add_or_replace_file", zipruby_archive_add_or_replace_file, -1);
87
93
  rb_define_method(Archive, "add_or_replace_filep", zipruby_archive_add_or_replace_filep, -1);
94
+ rb_define_method(Archive, "add_or_replace", zipruby_archive_add_or_replace_function, -1);
88
95
  rb_define_method(Archive, "update", zipruby_archive_update, 1);
89
96
  rb_define_method(Archive, "<<", zipruby_archive_add_filep, -1);
90
97
  rb_define_method(Archive, "get_comment", zipruby_archive_get_comment, -1);
@@ -541,6 +548,114 @@ static VALUE zipruby_archive_add_or_replace_filep(int argc, VALUE *argv, VALUE s
541
548
  }
542
549
  }
543
550
 
551
+ /* */
552
+ static VALUE zipruby_archive_add_function(int argc, VALUE *argv, VALUE self) {
553
+ VALUE name, mtime;
554
+ struct zipruby_archive *p_archive;
555
+ struct zip_source *zsource;
556
+ struct read_proc *z;
557
+
558
+ rb_scan_args(argc, argv, "11", &name, &mtime);
559
+ rb_need_block();
560
+
561
+ if (NIL_P(mtime)) {
562
+ mtime = rb_funcall(rb_cTime, rb_intern("now"), 0);
563
+ } else if (!rb_obj_is_instance_of(mtime, rb_cTime)) {
564
+ rb_raise(rb_eTypeError, "wrong argument type %s (expected Time)", rb_class2name(CLASS_OF(mtime)));
565
+ }
566
+
567
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
568
+ Check_Archive(p_archive);
569
+
570
+ if ((z = malloc(sizeof(struct read_proc))) == NULL) {
571
+ zip_unchange_all(p_archive->archive);
572
+ zip_unchange_archive(p_archive->archive);
573
+ rb_raise(rb_eRuntimeError, "Add failed - %s: Cannot allocate memory", StringValuePtr(name));
574
+ }
575
+
576
+ z->proc = rb_block_proc();
577
+ z->mtime = mtime;
578
+
579
+ if ((zsource = zip_source_proc(p_archive->archive, z)) == NULL) {
580
+ free(z);
581
+ rb_raise(Error, "Add failed - %s: %s", StringValuePtr(name), zip_strerror(p_archive->archive));
582
+ }
583
+
584
+ if (zip_add(p_archive->archive, StringValuePtr(name), zsource) == -1) {
585
+ zip_source_free(zsource);
586
+ zip_unchange_all(p_archive->archive);
587
+ zip_unchange_archive(p_archive->archive);
588
+ rb_raise(Error, "Add file failed - %s: %s", StringValuePtr(name), zip_strerror(p_archive->archive));
589
+ }
590
+
591
+ return Qnil;
592
+ }
593
+
594
+ /* */
595
+ static VALUE zipruby_archive_replace_function(int argc, VALUE *argv, VALUE self) {
596
+ VALUE index, mtime;
597
+ struct zipruby_archive *p_archive;
598
+ struct zip_source *zsource;
599
+ struct read_proc *z;
600
+
601
+ rb_scan_args(argc, argv, "11", &index, &mtime);
602
+ rb_need_block();
603
+ Check_Type(index, T_FIXNUM);
604
+
605
+ if (NIL_P(mtime)) {
606
+ mtime = rb_funcall(rb_cTime, rb_intern("now"), 0);
607
+ } else if (!rb_obj_is_instance_of(mtime, rb_cTime)) {
608
+ rb_raise(rb_eTypeError, "wrong argument type %s (expected Time)", rb_class2name(CLASS_OF(mtime)));
609
+ }
610
+
611
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
612
+ Check_Archive(p_archive);
613
+
614
+ if ((z = malloc(sizeof(struct read_proc))) == NULL) {
615
+ zip_unchange_all(p_archive->archive);
616
+ zip_unchange_archive(p_archive->archive);
617
+ rb_raise(rb_eRuntimeError, "Replace failed at %d: Cannot allocate memory", NUM2INT(index));
618
+ }
619
+
620
+ z->proc = rb_block_proc();
621
+ z->mtime = mtime;
622
+
623
+ if ((zsource = zip_source_proc(p_archive->archive, z)) == NULL) {
624
+ free(z);
625
+ rb_raise(Error, "Replace failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
626
+ }
627
+
628
+ if (zip_replace(p_archive->archive, NUM2INT(index), zsource) == -1) {
629
+ zip_source_free(zsource);
630
+ zip_unchange_all(p_archive->archive);
631
+ zip_unchange_archive(p_archive->archive);
632
+ rb_raise(Error, "Replace failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
633
+ }
634
+
635
+ return Qnil;
636
+ }
637
+
638
+ /* */
639
+ static VALUE zipruby_archive_add_or_replace_function(int argc, VALUE *argv, VALUE self) {
640
+ VALUE name, mtime;
641
+ struct zipruby_archive *p_archive;
642
+ int index;
643
+
644
+ rb_scan_args(argc, argv, "11", &name, &mtime);
645
+ Check_Type(name, T_STRING);
646
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
647
+ Check_Archive(p_archive);
648
+
649
+ index = zip_name_locate(p_archive->archive, StringValuePtr(name), ZIP_FL_NOCASE);
650
+
651
+ if (index >= 0) {
652
+ VALUE args[] = { INT2NUM(index), mtime };
653
+ return zipruby_archive_replace_function(2, args, self);
654
+ } else {
655
+ return zipruby_archive_add_function(argc, argv, self);
656
+ }
657
+ }
658
+
544
659
  /* */
545
660
  static VALUE zipruby_archive_update(VALUE self, VALUE srcarchive) {
546
661
  struct zipruby_archive *p_archive, *p_srcarchive;
@@ -1474,3 +1589,67 @@ void Init_zipruby_zip() {
1474
1589
  rb_define_const(Zip, "EM_TRAD_PKWARE", INT2NUM(ZIP_EM_TRAD_PKWARE));
1475
1590
  // XXX: Strong Encryption Header not parsed yet
1476
1591
  }
1592
+ #include <string.h>
1593
+
1594
+ #include "zip.h"
1595
+ #include "zipint.h"
1596
+ #include "zipruby_zip_source_proc.h"
1597
+ #include "ruby.h"
1598
+
1599
+ static ssize_t read_proc(void *state, void *data, size_t len, enum zip_source_cmd cmd) {
1600
+ struct read_proc *z;
1601
+ VALUE src;
1602
+ char *buf;
1603
+ size_t n;
1604
+
1605
+ z = (struct read_proc *) state;
1606
+ buf = (char *) data;
1607
+
1608
+ switch (cmd) {
1609
+ case ZIP_SOURCE_OPEN:
1610
+ return 0;
1611
+
1612
+ case ZIP_SOURCE_READ:
1613
+ src = rb_funcall(z->proc, rb_intern("call"), 1, ULONG2NUM(len));
1614
+
1615
+ if (NIL_P(src)) {
1616
+ return 0;
1617
+ }
1618
+
1619
+ src = rb_check_convert_type(src, T_STRING, "String", "to_s");
1620
+ n = RSTRING(src)->len;
1621
+
1622
+ if (n > 0) {
1623
+ n = (n > len) ? len : n;
1624
+ memcpy(buf, StringValuePtr(src), n);
1625
+ }
1626
+
1627
+ return n;
1628
+
1629
+ case ZIP_SOURCE_CLOSE:
1630
+ return 0;
1631
+
1632
+ case ZIP_SOURCE_STAT:
1633
+ {
1634
+ struct zip_stat *st = (struct zip_stat *)data;
1635
+ zip_stat_init(st);
1636
+ st->mtime = NUM2LONG(rb_funcall(z->mtime, rb_intern("tv_sec"), 0));
1637
+ return sizeof(*st);
1638
+ }
1639
+
1640
+ case ZIP_SOURCE_ERROR:
1641
+ return 0;
1642
+
1643
+ case ZIP_SOURCE_FREE:
1644
+ free(z);
1645
+ return 0;
1646
+ }
1647
+
1648
+ return -1;
1649
+ }
1650
+
1651
+ struct zip_source *zip_source_proc(struct zip *za, struct read_proc *z) {
1652
+ struct zip_source *zs;
1653
+ zs = zip_source_function(za, read_proc, z);
1654
+ return zs;
1655
+ }
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.2.3
4
+ version: 0.2.4
5
5
  platform: mswin32
6
6
  authors:
7
7
  - winebarrel