rubyzip 0.9.9 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (135) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +284 -41
  3. data/Rakefile +11 -6
  4. data/TODO +0 -1
  5. data/lib/zip/central_directory.rb +208 -0
  6. data/lib/zip/compressor.rb +1 -2
  7. data/lib/zip/constants.rb +59 -7
  8. data/lib/zip/crypto/encryption.rb +11 -0
  9. data/lib/zip/crypto/null_encryption.rb +43 -0
  10. data/lib/zip/crypto/traditional_encryption.rb +99 -0
  11. data/lib/zip/decompressor.rb +4 -4
  12. data/lib/zip/deflater.rb +17 -13
  13. data/lib/zip/dos_time.rb +13 -14
  14. data/lib/zip/entry.rb +700 -0
  15. data/lib/zip/entry_set.rb +86 -0
  16. data/lib/zip/errors.rb +18 -0
  17. data/lib/zip/extra_field/generic.rb +43 -0
  18. data/lib/zip/extra_field/ntfs.rb +90 -0
  19. data/lib/zip/extra_field/old_unix.rb +44 -0
  20. data/lib/zip/extra_field/universal_time.rb +47 -0
  21. data/lib/zip/extra_field/unix.rb +37 -0
  22. data/lib/zip/extra_field/zip64.rb +68 -0
  23. data/lib/zip/extra_field/zip64_placeholder.rb +15 -0
  24. data/lib/zip/extra_field.rb +101 -0
  25. data/lib/zip/file.rb +443 -0
  26. data/lib/zip/{zipfilesystem.rb → filesystem.rb} +162 -157
  27. data/lib/zip/inflater.rb +29 -28
  28. data/lib/zip/input_stream.rb +173 -0
  29. data/lib/zip/ioextras/abstract_input_stream.rb +111 -0
  30. data/lib/zip/ioextras/abstract_output_stream.rb +43 -0
  31. data/lib/zip/ioextras.rb +21 -149
  32. data/lib/zip/null_compressor.rb +2 -2
  33. data/lib/zip/null_decompressor.rb +8 -6
  34. data/lib/zip/null_input_stream.rb +3 -2
  35. data/lib/zip/output_stream.rb +189 -0
  36. data/lib/zip/pass_thru_compressor.rb +6 -6
  37. data/lib/zip/pass_thru_decompressor.rb +19 -19
  38. data/lib/zip/{zip_streamable_directory.rb → streamable_directory.rb} +3 -3
  39. data/lib/zip/streamable_stream.rb +56 -0
  40. data/lib/zip/version.rb +3 -0
  41. data/lib/zip.rb +71 -0
  42. data/samples/example.rb +44 -32
  43. data/samples/example_filesystem.rb +16 -18
  44. data/samples/example_recursive.rb +33 -28
  45. data/samples/{gtkRubyzip.rb → gtk_ruby_zip.rb} +26 -28
  46. data/samples/qtzip.rb +22 -31
  47. data/samples/write_simple.rb +12 -13
  48. data/samples/zipfind.rb +31 -39
  49. data/test/basic_zip_file_test.rb +60 -0
  50. data/test/case_sensitivity_test.rb +69 -0
  51. data/test/central_directory_entry_test.rb +69 -0
  52. data/test/central_directory_test.rb +100 -0
  53. data/test/crypto/null_encryption_test.rb +57 -0
  54. data/test/crypto/traditional_encryption_test.rb +80 -0
  55. data/test/data/WarnInvalidDate.zip +0 -0
  56. data/test/data/file1.txt +46 -0
  57. data/test/data/file1.txt.deflatedData +0 -0
  58. data/test/data/file2.txt +1504 -0
  59. data/test/data/globTest/foo/bar/baz/foo.txt +0 -0
  60. data/test/data/globTest/foo.txt +0 -0
  61. data/test/data/globTest/food.txt +0 -0
  62. data/test/data/globTest.zip +0 -0
  63. data/test/data/gpbit3stored.zip +0 -0
  64. data/test/data/mimetype +1 -0
  65. data/test/data/notzippedruby.rb +7 -0
  66. data/test/data/ntfs.zip +0 -0
  67. data/test/data/oddExtraField.zip +0 -0
  68. data/test/data/path_traversal/Makefile +10 -0
  69. data/test/data/path_traversal/jwilk/README.md +5 -0
  70. data/test/data/path_traversal/jwilk/absolute1.zip +0 -0
  71. data/test/data/path_traversal/jwilk/absolute2.zip +0 -0
  72. data/test/data/path_traversal/jwilk/dirsymlink.zip +0 -0
  73. data/test/data/path_traversal/jwilk/dirsymlink2a.zip +0 -0
  74. data/test/data/path_traversal/jwilk/dirsymlink2b.zip +0 -0
  75. data/test/data/path_traversal/jwilk/relative0.zip +0 -0
  76. data/test/data/path_traversal/jwilk/relative2.zip +0 -0
  77. data/test/data/path_traversal/jwilk/symlink.zip +0 -0
  78. data/test/data/path_traversal/relative1.zip +0 -0
  79. data/test/data/path_traversal/tilde.zip +0 -0
  80. data/test/data/path_traversal/tuzovakaoff/README.md +3 -0
  81. data/test/data/path_traversal/tuzovakaoff/absolutepath.zip +0 -0
  82. data/test/data/path_traversal/tuzovakaoff/symlink.zip +0 -0
  83. data/test/data/rubycode.zip +0 -0
  84. data/test/data/rubycode2.zip +0 -0
  85. data/test/data/test.xls +0 -0
  86. data/test/data/testDirectory.bin +0 -0
  87. data/test/data/zip64-sample.zip +0 -0
  88. data/test/data/zipWithDirs.zip +0 -0
  89. data/test/data/zipWithEncryption.zip +0 -0
  90. data/test/deflater_test.rb +65 -0
  91. data/test/encryption_test.rb +42 -0
  92. data/test/entry_set_test.rb +163 -0
  93. data/test/entry_test.rb +154 -0
  94. data/test/errors_test.rb +35 -0
  95. data/test/extra_field_test.rb +76 -0
  96. data/test/file_extract_directory_test.rb +54 -0
  97. data/test/file_extract_test.rb +145 -0
  98. data/test/file_permissions_test.rb +65 -0
  99. data/test/file_split_test.rb +57 -0
  100. data/test/file_test.rb +666 -0
  101. data/test/filesystem/dir_iterator_test.rb +58 -0
  102. data/test/filesystem/directory_test.rb +139 -0
  103. data/test/filesystem/file_mutating_test.rb +87 -0
  104. data/test/filesystem/file_nonmutating_test.rb +508 -0
  105. data/test/filesystem/file_stat_test.rb +64 -0
  106. data/test/gentestfiles.rb +126 -0
  107. data/test/inflater_test.rb +14 -0
  108. data/test/input_stream_test.rb +182 -0
  109. data/test/ioextras/abstract_input_stream_test.rb +102 -0
  110. data/test/ioextras/abstract_output_stream_test.rb +106 -0
  111. data/test/ioextras/fake_io_test.rb +18 -0
  112. data/test/local_entry_test.rb +154 -0
  113. data/test/output_stream_test.rb +128 -0
  114. data/test/pass_thru_compressor_test.rb +30 -0
  115. data/test/pass_thru_decompressor_test.rb +14 -0
  116. data/test/path_traversal_test.rb +141 -0
  117. data/test/samples/example_recursive_test.rb +37 -0
  118. data/test/settings_test.rb +95 -0
  119. data/test/test_helper.rb +234 -0
  120. data/test/unicode_file_names_and_comments_test.rb +62 -0
  121. data/test/zip64_full_test.rb +51 -0
  122. data/test/zip64_support_test.rb +14 -0
  123. metadata +274 -41
  124. data/NEWS +0 -172
  125. data/lib/zip/settings.rb +0 -10
  126. data/lib/zip/tempfile_bugfixed.rb +0 -195
  127. data/lib/zip/zip.rb +0 -56
  128. data/lib/zip/zip_central_directory.rb +0 -135
  129. data/lib/zip/zip_entry.rb +0 -638
  130. data/lib/zip/zip_entry_set.rb +0 -77
  131. data/lib/zip/zip_extra_field.rb +0 -213
  132. data/lib/zip/zip_file.rb +0 -340
  133. data/lib/zip/zip_input_stream.rb +0 -144
  134. data/lib/zip/zip_output_stream.rb +0 -173
  135. data/lib/zip/zip_streamable_stream.rb +0 -47
@@ -1,173 +0,0 @@
1
- module Zip
2
- # ZipOutputStream is the basic class for writing zip files. It is
3
- # possible to create a ZipOutputStream object directly, passing
4
- # the zip file name to the constructor, but more often than not
5
- # the ZipOutputStream will be obtained from a ZipFile (perhaps using the
6
- # ZipFileSystem interface) object for a particular entry in the zip
7
- # archive.
8
- #
9
- # A ZipOutputStream inherits IOExtras::AbstractOutputStream in order
10
- # to provide an IO-like interface for writing to a single zip
11
- # entry. Beyond methods for mimicking an IO-object it contains
12
- # the method put_next_entry that closes the current entry
13
- # and creates a new.
14
- #
15
- # Please refer to ZipInputStream for example code.
16
- #
17
- # java.util.zip.ZipOutputStream is the original inspiration for this
18
- # class.
19
-
20
- class ZipOutputStream
21
- include IOExtras::AbstractOutputStream
22
-
23
- attr_accessor :comment
24
-
25
- # Opens the indicated zip file. If a file with that name already
26
- # exists it will be overwritten.
27
- def initialize(fileName, stream=false)
28
- super()
29
- @fileName = fileName
30
- if stream
31
- @outputStream = StringIO.new
32
- else
33
- @outputStream = ::File.new(@fileName, "wb")
34
- end
35
- @entrySet = ZipEntrySet.new
36
- @compressor = NullCompressor.instance
37
- @closed = false
38
- @currentEntry = nil
39
- @comment = nil
40
- end
41
-
42
- # Same as #initialize but if a block is passed the opened
43
- # stream is passed to the block and closed when the block
44
- # returns.
45
- def ZipOutputStream.open(fileName)
46
- return new(fileName) unless block_given?
47
- zos = new(fileName)
48
- yield zos
49
- ensure
50
- zos.close if zos
51
- end
52
-
53
- # Same as #open but writes to a filestream instead
54
- def ZipOutputStream.write_buffer
55
- zos = new('', true)
56
- yield zos
57
- return zos.close_buffer
58
- end
59
-
60
- # Closes the stream and writes the central directory to the zip file
61
- def close
62
- return if @closed
63
- finalize_current_entry
64
- update_local_headers
65
- write_central_directory
66
- @outputStream.close
67
- @closed = true
68
- end
69
-
70
- # Closes the stream and writes the central directory to the zip file
71
- def close_buffer
72
- return @outputStream if @closed
73
- finalize_current_entry
74
- update_local_headers
75
- write_central_directory
76
- @closed = true
77
- @outputStream
78
- end
79
-
80
- # Closes the current entry and opens a new for writing.
81
- # +entry+ can be a ZipEntry object or a string.
82
- def put_next_entry(entryname, comment = nil, extra = nil, compression_method = ZipEntry::DEFLATED, level = Zlib::DEFAULT_COMPRESSION)
83
- raise ZipError, "zip stream is closed" if @closed
84
- if entryname.kind_of?(ZipEntry)
85
- new_entry = entryname
86
- else
87
- new_entry = ZipEntry.new(@fileName, entryname.to_s)
88
- end
89
- new_entry.comment = comment if !comment.nil?
90
- if (!extra.nil?)
91
- new_entry.extra = ZipExtraField === extra ? extra : ZipExtraField.new(extra.to_s)
92
- end
93
- new_entry.compression_method = compression_method if !compression_method.nil?
94
- init_next_entry(new_entry, level)
95
- @currentEntry = new_entry
96
- end
97
-
98
- def copy_raw_entry(entry)
99
- entry = entry.dup
100
- raise ZipError, "zip stream is closed" if @closed
101
- raise ZipError, "entry is not a ZipEntry" if !entry.kind_of?(ZipEntry)
102
- finalize_current_entry
103
- @entrySet << entry
104
- src_pos = entry.local_entry_offset
105
- entry.write_local_entry(@outputStream)
106
- @compressor = NullCompressor.instance
107
- entry.get_raw_input_stream do |is|
108
- is.seek(src_pos, IO::SEEK_SET)
109
- IOExtras.copy_stream_n(@outputStream, is, entry.compressed_size)
110
- end
111
- @compressor = NullCompressor.instance
112
- @currentEntry = nil
113
- end
114
-
115
- private
116
-
117
- def finalize_current_entry
118
- return unless @currentEntry
119
- finish
120
- @currentEntry.compressed_size = @outputStream.tell - @currentEntry.localHeaderOffset - @currentEntry.calculate_local_header_size
121
- @currentEntry.size = @compressor.size
122
- @currentEntry.crc = @compressor.crc
123
- @currentEntry = nil
124
- @compressor = NullCompressor.instance
125
- end
126
-
127
- def init_next_entry(entry, level = Zlib::DEFAULT_COMPRESSION)
128
- finalize_current_entry
129
- @entrySet << entry
130
- entry.write_local_entry(@outputStream)
131
- @compressor = get_compressor(entry, level)
132
- end
133
-
134
- def get_compressor(entry, level)
135
- case entry.compression_method
136
- when ZipEntry::DEFLATED then Deflater.new(@outputStream, level)
137
- when ZipEntry::STORED then PassThruCompressor.new(@outputStream)
138
- else raise ZipCompressionMethodError,
139
- "Invalid compression method: '#{entry.compression_method}'"
140
- end
141
- end
142
-
143
- def update_local_headers
144
- pos = @outputStream.pos
145
- @entrySet.each do |entry|
146
- @outputStream.pos = entry.localHeaderOffset
147
- entry.write_local_entry(@outputStream)
148
- end
149
- @outputStream.pos = pos
150
- end
151
-
152
- def write_central_directory
153
- cdir = ZipCentralDirectory.new(@entrySet, @comment)
154
- cdir.write_to_stream(@outputStream)
155
- end
156
-
157
- protected
158
-
159
- def finish
160
- @compressor.finish
161
- end
162
-
163
- public
164
- # Modeled after IO.<<
165
- def << (data)
166
- @compressor << data
167
- end
168
- end
169
- end
170
-
171
- # Copyright (C) 2002, 2003 Thomas Sondergaard
172
- # rubyzip is free software; you can redistribute it and/or
173
- # modify it under the terms of the ruby license.
@@ -1,47 +0,0 @@
1
- module Zip
2
- class ZipStreamableStream < DelegateClass(ZipEntry) #nodoc:all
3
- def initialize(entry)
4
- super(entry)
5
- @tempFile = Tempfile.new(::File.basename(name), ::File.dirname(zipfile))
6
- @tempFile.binmode
7
- end
8
-
9
- def get_output_stream
10
- if block_given?
11
- begin
12
- yield(@tempFile)
13
- ensure
14
- @tempFile.close
15
- end
16
- else
17
- @tempFile
18
- end
19
- end
20
-
21
- def get_input_stream
22
- if ! @tempFile.closed?
23
- raise StandardError, "cannot open entry for reading while its open for writing - #{name}"
24
- end
25
- @tempFile.open # reopens tempfile from top
26
- @tempFile.binmode
27
- if block_given?
28
- begin
29
- yield(@tempFile)
30
- ensure
31
- @tempFile.close
32
- end
33
- else
34
- @tempFile
35
- end
36
- end
37
-
38
- def write_to_zip_output_stream(aZipOutputStream)
39
- aZipOutputStream.put_next_entry(self)
40
- get_input_stream { |is| IOExtras.copy_stream(aZipOutputStream, is) }
41
- end
42
- end
43
- end
44
-
45
- # Copyright (C) 2002, 2003 Thomas Sondergaard
46
- # rubyzip is free software; you can redistribute it and/or
47
- # modify it under the terms of the ruby license.