rubyzip 0.9.9 → 1.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of rubyzip might be problematic. Click here for more details.

Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/NEWS +9 -5
  3. data/README.md +79 -21
  4. data/Rakefile +1 -1
  5. data/lib/zip.rb +52 -0
  6. data/lib/zip/central_directory.rb +135 -0
  7. data/lib/zip/constants.rb +57 -7
  8. data/lib/zip/decompressor.rb +2 -2
  9. data/lib/zip/deflater.rb +11 -12
  10. data/lib/zip/dos_time.rb +9 -9
  11. data/lib/zip/entry.rb +609 -0
  12. data/lib/zip/entry_set.rb +86 -0
  13. data/lib/zip/errors.rb +8 -0
  14. data/lib/zip/extra_field.rb +90 -0
  15. data/lib/zip/extra_field/generic.rb +43 -0
  16. data/lib/zip/extra_field/universal_time.rb +47 -0
  17. data/lib/zip/extra_field/unix.rb +39 -0
  18. data/lib/zip/{zip_file.rb → file.rb} +140 -61
  19. data/lib/zip/{zipfilesystem.rb → filesystem.rb} +12 -12
  20. data/lib/zip/inflater.rb +24 -24
  21. data/lib/zip/{zip_input_stream.rb → input_stream.rb} +11 -10
  22. data/lib/zip/ioextras.rb +145 -123
  23. data/lib/zip/null_compressor.rb +1 -1
  24. data/lib/zip/null_decompressor.rb +5 -3
  25. data/lib/zip/null_input_stream.rb +2 -2
  26. data/lib/zip/{zip_output_stream.rb → output_stream.rb} +43 -41
  27. data/lib/zip/pass_thru_compressor.rb +2 -2
  28. data/lib/zip/pass_thru_decompressor.rb +17 -16
  29. data/lib/zip/{zip_streamable_directory.rb → streamable_directory.rb} +1 -1
  30. data/lib/zip/{zip_streamable_stream.rb → streamable_stream.rb} +2 -2
  31. data/lib/zip/version.rb +3 -0
  32. data/samples/example.rb +27 -5
  33. data/samples/example_filesystem.rb +2 -2
  34. data/samples/example_recursive.rb +1 -1
  35. data/samples/gtkRubyzip.rb +1 -1
  36. data/samples/qtzip.rb +2 -2
  37. data/samples/write_simple.rb +1 -1
  38. data/samples/zipfind.rb +1 -1
  39. metadata +29 -27
  40. data/lib/zip/settings.rb +0 -10
  41. data/lib/zip/tempfile_bugfixed.rb +0 -195
  42. data/lib/zip/zip.rb +0 -56
  43. data/lib/zip/zip_central_directory.rb +0 -135
  44. data/lib/zip/zip_entry.rb +0 -638
  45. data/lib/zip/zip_entry_set.rb +0 -77
  46. data/lib/zip/zip_extra_field.rb +0 -213
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 43c105816327e51ada812102f96fb5ee767f4111
4
+ data.tar.gz: 9885633481d4b7f0887a5d7861e99fb8a632e913
5
+ SHA512:
6
+ metadata.gz: 5d5b39710bf4d51ffeff8d4de6a9b1583f5fc844c1a128014a591288d3e770d20fb89a600884cfac0795dedbea0caea37a3af1b407f477a504923f719a4be158
7
+ data.tar.gz: 6549a23e186d61d456c3214e627dd084b80a569142bc0994c8dc9e2e87dcdfe7059f027d7ff045287c6daa73e95391c7247bc36fb0da4562e95a3f0fcd615b8c
data/NEWS CHANGED
@@ -8,6 +8,10 @@ even though there is actually a comment.
8
8
 
9
9
  Fixed: "Unitialized constant NullInputStream" error
10
10
 
11
+ = Version 0.9.5
12
+
13
+ Removed support for loading ruby in zip files (ziprequire.rb).
14
+
11
15
  = Version 0.9.4
12
16
 
13
17
  Changed ZipOutputStream.put_next_entry signature (API CHANGE!). Now
@@ -103,11 +107,11 @@ Fixed ZipFile.get_output_stream bug.
103
107
 
104
108
  List of changes:
105
109
  * Ruby 1.8.0 and ruby-zlib 0.6.0 compatibility
106
- * Changed method names from camelCase to rubys underscore style.
110
+ * Changed method names from camelCase to rubys underscore style.
107
111
  * Installs to zip/ subdir instead of directly to site_ruby
108
112
  * Added ZipFile.directory and ZipFile.file - each method return an
109
113
  object that can be used like Dir and File only for the contents of the
110
- zip file.
114
+ zip file.
111
115
  * Added sample application zipfind which works like Find.find, only
112
116
  Zip::ZipFind.find traverses into zip archives too.
113
117
 
@@ -122,11 +126,11 @@ modules from a zip file, in a fashion similar to jar files in
122
126
  Java. Added gtkRubyzip, another sample application. Implemented
123
127
  ZipInputStream.lineno and ZipInputStream.rewind
124
128
 
125
- Bug fixes:
129
+ Bug fixes:
126
130
 
127
- * Read and write date and time information correctly for zip entries.
131
+ * Read and write date and time information correctly for zip entries.
128
132
  * Fixed read() using separate buffer, causing mix of gets/readline/read to
129
- cause problems.
133
+ cause problems.
130
134
 
131
135
  = Version 0.4.2
132
136
 
data/README.md CHANGED
@@ -1,7 +1,12 @@
1
- # rubyzip [![Build Status](https://secure.travis-ci.org/aussiegeek/rubyzip.png)](http://travis-ci.org/aussiegeek/rubyzip)
1
+ # rubyzip [![Build Status](https://secure.travis-ci.org/rubyzip/rubyzip.png)](http://travis-ci.org/aussiegeek/rubyzip)[![Code Climate](https://codeclimate.com/github/aussiegeek/rubyzip.png)](https://codeclimate.com/github/aussiegeek/rubyzip)
2
2
 
3
3
  rubyzip is a ruby library for reading and writing zip files.
4
4
 
5
+ ## Important note
6
+
7
+ This branch for next version of rubyzip. Do not use it on production.
8
+ Rubyzip interface will be changed!!!
9
+
5
10
  ## Installation
6
11
  rubyzip is available on RubyGems, so:
7
12
 
@@ -21,14 +26,14 @@ gem 'rubyzip'
21
26
 
22
27
  ```ruby
23
28
  require 'rubygems'
24
- require 'zip/zip'
25
-
29
+ require 'zip'
30
+
26
31
  folder = "Users/me/Desktop/stuff_to_zip"
27
32
  input_filenames = ['image.jpg', 'description.txt', 'stats.csv']
28
-
33
+
29
34
  zipfile_name = "/Users/me/Desktop/archive.zip"
30
-
31
- Zip::ZipFile.open(zipfile_name, Zip::ZipFile::CREATE) do |zipfile|
35
+
36
+ Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile|
32
37
  input_filenames.each do |filename|
33
38
  # Two arguments:
34
39
  # - The name of the file as it will appear in the archive
@@ -38,31 +43,68 @@ Zip::ZipFile.open(zipfile_name, Zip::ZipFile::CREATE) do |zipfile|
38
43
  end
39
44
  ```
40
45
 
46
+ ### Zipping a directory recursively
47
+
48
+ ```ruby
49
+ require 'rubygems'
50
+ require 'zip'
51
+
52
+ directory = '/Users/me/Desktop/directory_to_zip/'
53
+ zipfile_name = '/Users/me/Desktop/recursive_directory.zip'
54
+
55
+ Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile|
56
+ Dir[File.join(directory, '**', '**')].each do |file|
57
+ zipfile.add(file.sub(directory, ''), file)
58
+ end
59
+ end
60
+ ```
61
+
62
+ ## Known issues
63
+
64
+ ### Modify docx file with rubyzip
65
+
66
+ Use `write_buffer` instead `open`. Thanks to @jondruse
67
+
68
+ ```ruby
69
+ buffer = Zip::OutputStream.write_buffer do |out|
70
+ @zip_file.entries.each do |e|
71
+ unless [DOCUMENT_FILE_PATH, RELS_FILE_PATH].include?(e.name)
72
+ out.put_next_entry(e.name)
73
+ out.write e.get_input_stream.read
74
+ end
75
+ end
76
+
77
+ out.put_next_entry(DOCUMENT_FILE_PATH)
78
+ out.write xml_doc.to_xml(:indent => 0).gsub("\n","")
79
+
80
+ out.put_next_entry(RELS_FILE_PATH)
81
+ out.write rels.to_xml(:indent => 0).gsub("\n","")
82
+ end
83
+
84
+ File.open(new_path, "w") {|f| f.write(buffer.string) }
85
+ ```
86
+
41
87
  ## Further Documentation
42
88
 
43
89
  There is more than one way to access or create a zip archive with
44
90
  rubyzip. The basic API is modeled after the classes in
45
91
  java.util.zip from the Java SDK. This means there are classes such
46
- as Zip::ZipInputStream, Zip::ZipOutputStream and
47
- Zip::ZipFile. Zip::ZipInputStream provides a basic interface for
92
+ as Zip::InputStream, Zip::OutputStream and
93
+ Zip::File. Zip::InputStream provides a basic interface for
48
94
  iterating through the entries in a zip archive and reading from the
49
95
  entries in the same way as from a regular File or IO
50
- object. ZipOutputStream is the corresponding basic output
51
- facility. Zip::ZipFile provides a mean for accessing the archives
96
+ object. OutputStream is the corresponding basic output
97
+ facility. Zip::File provides a mean for accessing the archives
52
98
  central directory and provides means for accessing any entry without
53
99
  having to iterate through the archive. Unlike Java's
54
- java.util.zip.ZipFile rubyzip's Zip::ZipFile is mutable, which means
100
+ java.util.zip.ZipFile rubyzip's Zip::File is mutable, which means
55
101
  it can be used to change zip files as well.
56
102
 
57
103
  Another way to access a zip archive with rubyzip is to use rubyzip's
58
- Zip::ZipFileSystem API. Using this API files can be read from and
104
+ Zip::FileSystem API. Using this API files can be read from and
59
105
  written to the archive in much the same manner as ruby's builtin
60
106
  classes allows files to be read from and written to the file system.
61
107
 
62
- rubyzip also features the
63
- zip/ziprequire.rb[link:files/lib/zip/ziprequire_rb.html] module which
64
- allows ruby to load ruby modules from zip archives.
65
-
66
108
  For details about the specific behaviour of classes and methods refer
67
109
  to the test suite. Finally you can generate the rdoc documentation or
68
110
  visit http://rubyzip.sourceforge.net.
@@ -72,16 +114,32 @@ visit http://rubyzip.sourceforge.net.
72
114
 
73
115
  By default, rubyzip will not overwrite files if they already exist inside of the extracted path. To change this behavior, you may specify a configuration option like so:
74
116
 
75
- ```
76
- Zip.options[:on_exists_proc] = true
117
+ ```ruby
118
+ Zip.on_exists_proc = true
77
119
  ```
78
120
 
79
121
  If you're using rubyzip with rails, consider placing this snippet of code in an initializer file such as `config/initializers/rubyzip.rb`
80
122
 
81
123
  Additionally, if you want to configure rubyzip to overwrite existing files while creating a .zip file, you can do so with the following:
82
124
 
125
+ ```ruby
126
+ Zip.continue_on_exists_proc = true
127
+ ```
128
+
129
+ If you want to store non english names and want to open properly file on Windows(pre 7) you need to set next option:
130
+
131
+ ```ruby
132
+ Zip.unicode_names = true
83
133
  ```
84
- Zip.options[:continue_on_exists_proc] = true
134
+
135
+ All settings in same time
136
+
137
+ ```ruby
138
+ Zip.setup do |c|
139
+ c.on_exists_proc = true
140
+ c.continue_on_exists_proc = true
141
+ c.unicode_names = true
142
+ end
85
143
  ```
86
144
 
87
145
  ## Developing
@@ -95,9 +153,9 @@ rake
95
153
 
96
154
  ## Website and Project Home
97
155
 
98
- http://github.com/aussiegeek/rubyzip
156
+ http://github.com/rubyzip/rubyzip
99
157
 
100
- http://rdoc.info/github/aussiegeek/rubyzip/master/frames
158
+ http://rdoc.info/github/rubyzip/rubyzip/master/frames
101
159
 
102
160
  ## Authors
103
161
 
data/Rakefile CHANGED
@@ -1,4 +1,4 @@
1
- require 'rubygems'
1
+ require 'bundler/gem_tasks'
2
2
  require 'rake/testtask'
3
3
 
4
4
  task :default => [:test]
@@ -0,0 +1,52 @@
1
+ require 'delegate'
2
+ require 'singleton'
3
+ require 'tempfile'
4
+ require 'fileutils'
5
+ require 'stringio'
6
+ require 'zlib'
7
+ require 'zip/dos_time'
8
+ require 'zip/ioextras'
9
+ require 'rbconfig'
10
+ require 'zip/entry'
11
+ require 'zip/extra_field'
12
+ require 'zip/entry_set'
13
+ require 'zip/central_directory'
14
+ require 'zip/file'
15
+ require 'zip/input_stream'
16
+ require 'zip/output_stream'
17
+ require 'zip/decompressor'
18
+ require 'zip/compressor'
19
+ require 'zip/null_decompressor'
20
+ require 'zip/null_compressor'
21
+ require 'zip/null_input_stream'
22
+ require 'zip/pass_thru_compressor'
23
+ require 'zip/pass_thru_decompressor'
24
+ require 'zip/inflater'
25
+ require 'zip/deflater'
26
+ require 'zip/streamable_stream'
27
+ require 'zip/streamable_directory'
28
+ require 'zip/constants'
29
+ require 'zip/errors'
30
+
31
+ module Zip
32
+ extend self
33
+ attr_accessor :unicode_names, :on_exists_proc, :continue_on_exists_proc
34
+
35
+ def reset!
36
+ @_ran_once = false
37
+ @unicode_names = false
38
+ @on_exists_proc = false
39
+ @continue_on_exists_proc = false
40
+ end
41
+
42
+ def setup
43
+ yield self unless @_ran_once
44
+ @_ran_once = true
45
+ end
46
+
47
+ reset!
48
+ end
49
+
50
+ # Copyright (C) 2002, 2003 Thomas Sondergaard
51
+ # rubyzip is free software; you can redistribute it and/or
52
+ # modify it under the terms of the ruby license.
@@ -0,0 +1,135 @@
1
+ module Zip
2
+ class CentralDirectory
3
+ include Enumerable
4
+
5
+ END_OF_CENTRAL_DIRECTORY_SIGNATURE = 0x06054b50
6
+ MAX_END_OF_CENTRAL_DIRECTORY_STRUCTURE_SIZE = 65536 + 18
7
+ STATIC_EOCD_SIZE = 22
8
+
9
+ attr_reader :comment
10
+
11
+ # Returns an Enumerable containing the entries.
12
+ def entries
13
+ @entry_set.entries
14
+ end
15
+
16
+ def initialize(entries = EntrySet.new, comment = "") #:nodoc:
17
+ super()
18
+ @entry_set = entries.kind_of?(EntrySet) ? entries : EntrySet.new(entries)
19
+ @comment = comment
20
+ end
21
+
22
+ def write_to_stream(io) #:nodoc:
23
+ offset = io.tell
24
+ @entry_set.each { |entry| entry.write_c_dir_entry(io) }
25
+ write_e_o_c_d(io, offset)
26
+ end
27
+
28
+ def write_e_o_c_d(io, offset) #:nodoc:
29
+ tmp = [
30
+ END_OF_CENTRAL_DIRECTORY_SIGNATURE,
31
+ 0, # @numberOfThisDisk
32
+ 0, # @numberOfDiskWithStartOfCDir
33
+ @entry_set ? @entry_set.size : 0,
34
+ @entry_set ? @entry_set.size : 0,
35
+ cdir_size,
36
+ offset,
37
+ @comment ? @comment.length : 0
38
+ ]
39
+ io << tmp.pack('VvvvvVVv')
40
+ io << @comment
41
+ end
42
+
43
+ private :write_e_o_c_d
44
+
45
+ def cdir_size #:nodoc:
46
+ # does not include eocd
47
+ @entry_set.inject(0) do |value, entry|
48
+ entry.cdir_header_size + value
49
+ end
50
+ end
51
+
52
+ private :cdir_size
53
+
54
+ def read_e_o_c_d(io) #:nodoc:
55
+ buf = get_e_o_c_d(io)
56
+ @numberOfThisDisk = Entry.read_zip_short(buf)
57
+ @numberOfDiskWithStartOfCDir = Entry.read_zip_short(buf)
58
+ @totalNumberOfEntriesInCDirOnThisDisk = Entry.read_zip_short(buf)
59
+ @size = Entry.read_zip_short(buf)
60
+ @sizeInBytes = Entry.read_zip_long(buf)
61
+ @cdirOffset = Entry.read_zip_long(buf)
62
+ commentLength = Entry.read_zip_short(buf)
63
+ if commentLength <= 0
64
+ @comment = buf.slice!(0, buf.size)
65
+ else
66
+ @comment = buf.read(commentLength)
67
+ end
68
+ raise ZipError, "Zip consistency problem while reading eocd structure" unless buf.size == 0
69
+ end
70
+
71
+ def read_central_directory_entries(io) #:nodoc:
72
+ begin
73
+ io.seek(@cdirOffset, IO::SEEK_SET)
74
+ rescue Errno::EINVAL
75
+ raise ZipError, "Zip consistency problem while reading central directory entry"
76
+ end
77
+ @entry_set = EntrySet.new
78
+ @size.times do
79
+ tmp = Entry.read_c_dir_entry(io)
80
+ @entry_set << tmp
81
+ end
82
+ end
83
+
84
+ def read_from_stream(io) #:nodoc:
85
+ read_e_o_c_d(io)
86
+ read_central_directory_entries(io)
87
+ end
88
+
89
+ def get_e_o_c_d(io) #:nodoc:
90
+ begin
91
+ io.seek(-MAX_END_OF_CENTRAL_DIRECTORY_STRUCTURE_SIZE, IO::SEEK_END)
92
+ rescue Errno::EINVAL
93
+ io.seek(0, IO::SEEK_SET)
94
+ end
95
+ buf = io.read
96
+ sigIndex = buf.rindex([END_OF_CENTRAL_DIRECTORY_SIGNATURE].pack('V'))
97
+ raise ZipError, "Zip end of central directory signature not found" unless sigIndex
98
+ buf = buf.slice!((sigIndex + 4)..(buf.bytesize))
99
+
100
+ def buf.read(count)
101
+ slice!(0, count)
102
+ end
103
+
104
+ buf
105
+ end
106
+
107
+ # For iterating over the entries.
108
+ def each(&proc)
109
+ @entry_set.each(&proc)
110
+ end
111
+
112
+ # Returns the number of entries in the central directory (and
113
+ # consequently in the zip archive).
114
+ def size
115
+ @entry_set.size
116
+ end
117
+
118
+ def CentralDirectory.read_from_stream(io) #:nodoc:
119
+ cdir = new
120
+ cdir.read_from_stream(io)
121
+ return cdir
122
+ rescue ZipError
123
+ return nil
124
+ end
125
+
126
+ def ==(other) #:nodoc:
127
+ return false unless other.kind_of?(CentralDirectory)
128
+ @entry_set.entries.sort == other.entries.sort && comment == other.comment
129
+ end
130
+ end
131
+ end
132
+
133
+ # Copyright (C) 2002, 2003 Thomas Sondergaard
134
+ # rubyzip is free software; you can redistribute it and/or
135
+ # modify it under the terms of the ruby license.
@@ -1,11 +1,61 @@
1
1
  module Zip
2
- VERSION = '0.9.7'
3
- RUBY_MINOR_VERSION = RUBY_VERSION.split(".")[1].to_i
4
2
  RUNNING_ON_WINDOWS = RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/i
5
3
 
6
- # Ruby 1.7.x compatibility
7
- # In ruby 1.6.x and 1.8.0 reading from an empty stream returns
8
- # an empty string the first time and then nil.
9
- # not so in 1.7.x
10
- EMPTY_FILE_RETURNS_EMPTY_STRING_FIRST = RUBY_MINOR_VERSION != 7
4
+ CENTRAL_DIRECTORY_ENTRY_SIGNATURE = 0x02014b50
5
+ CDIR_ENTRY_STATIC_HEADER_LENGTH = 46
6
+
7
+ LOCAL_ENTRY_SIGNATURE = 0x04034b50
8
+ LOCAL_ENTRY_STATIC_HEADER_LENGTH = 30
9
+ LOCAL_ENTRY_TRAILING_DESCRIPTOR_LENGTH = 4+4+4
10
+ VERSION_NEEDED_TO_EXTRACT = 20
11
+
12
+ FILE_TYPE_FILE = 010
13
+ FILE_TYPE_DIR = 004
14
+ FILE_TYPE_SYMLINK = 012
15
+
16
+ FSTYPE_FAT = 0
17
+ FSTYPE_AMIGA = 1
18
+ FSTYPE_VMS = 2
19
+ FSTYPE_UNIX = 3
20
+ FSTYPE_VM_CMS = 4
21
+ FSTYPE_ATARI = 5
22
+ FSTYPE_HPFS = 6
23
+ FSTYPE_MAC = 7
24
+ FSTYPE_Z_SYSTEM = 8
25
+ FSTYPE_CPM = 9
26
+ FSTYPE_TOPS20 = 10
27
+ FSTYPE_NTFS = 11
28
+ FSTYPE_QDOS = 12
29
+ FSTYPE_ACORN = 13
30
+ FSTYPE_VFAT = 14
31
+ FSTYPE_MVS = 15
32
+ FSTYPE_BEOS = 16
33
+ FSTYPE_TANDEM = 17
34
+ FSTYPE_THEOS = 18
35
+ FSTYPE_MAC_OSX = 19
36
+ FSTYPE_ATHEOS = 30
37
+
38
+ FSTYPES = {
39
+ FSTYPE_FAT => 'FAT'.freeze,
40
+ FSTYPE_AMIGA => 'Amiga'.freeze,
41
+ FSTYPE_VMS => 'VMS (Vax or Alpha AXP)'.freeze,
42
+ FSTYPE_UNIX => 'Unix'.freeze,
43
+ FSTYPE_VM_CMS => 'VM/CMS'.freeze,
44
+ FSTYPE_ATARI => 'Atari ST'.freeze,
45
+ FSTYPE_HPFS => 'OS/2 or NT HPFS'.freeze,
46
+ FSTYPE_MAC => 'Macintosh'.freeze,
47
+ FSTYPE_Z_SYSTEM => 'Z-System'.freeze,
48
+ FSTYPE_CPM => 'CP/M'.freeze,
49
+ FSTYPE_TOPS20 => 'TOPS-20'.freeze,
50
+ FSTYPE_NTFS => 'NTFS'.freeze,
51
+ FSTYPE_QDOS => 'SMS/QDOS'.freeze,
52
+ FSTYPE_ACORN => 'Acorn RISC OS'.freeze,
53
+ FSTYPE_VFAT => 'Win32 VFAT'.freeze,
54
+ FSTYPE_MVS => 'MVS'.freeze,
55
+ FSTYPE_BEOS => 'BeOS'.freeze,
56
+ FSTYPE_TANDEM => 'Tandem NSK'.freeze,
57
+ FSTYPE_THEOS => 'Theos'.freeze,
58
+ FSTYPE_MAC_OSX => 'Mac OS/X (Darwin)'.freeze,
59
+ FSTYPE_ATHEOS => 'AtheOS'.freeze,
60
+ }.freeze
11
61
  end