hotplate 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +4 -0
  3. data/Rakefile +1 -0
  4. data/bin/toast +2 -0
  5. data/hotplate.gemspec +30 -0
  6. data/lib/hotplate/cli/build.rb +51 -0
  7. data/lib/hotplate/cli/init.rb +64 -0
  8. data/lib/hotplate/cli/main.rb +39 -0
  9. data/lib/hotplate/gems/rubyzip-1.1.7/README.md +271 -0
  10. data/lib/hotplate/gems/rubyzip-1.1.7/Rakefile +19 -0
  11. data/lib/hotplate/gems/rubyzip-1.1.7/TODO +15 -0
  12. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/central_directory.rb +208 -0
  13. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/compressor.rb +10 -0
  14. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/constants.rb +63 -0
  15. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/crypto/encryption.rb +11 -0
  16. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/crypto/null_encryption.rb +45 -0
  17. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/crypto/traditional_encryption.rb +99 -0
  18. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/decompressor.rb +13 -0
  19. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/deflater.rb +32 -0
  20. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/dos_time.rb +49 -0
  21. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/entry.rb +696 -0
  22. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/entry_set.rb +86 -0
  23. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/errors.rb +16 -0
  24. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/extra_field/generic.rb +43 -0
  25. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/extra_field/ntfs.rb +92 -0
  26. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/extra_field/old_unix.rb +45 -0
  27. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/extra_field/universal_time.rb +47 -0
  28. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/extra_field/unix.rb +38 -0
  29. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/extra_field/zip64.rb +67 -0
  30. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/extra_field/zip64_placeholder.rb +16 -0
  31. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/extra_field.rb +101 -0
  32. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/file.rb +436 -0
  33. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/filesystem.rb +626 -0
  34. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/inflater.rb +66 -0
  35. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/input_stream.rb +164 -0
  36. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/ioextras/abstract_input_stream.rb +115 -0
  37. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/ioextras/abstract_output_stream.rb +45 -0
  38. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/ioextras.rb +38 -0
  39. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/null_compressor.rb +15 -0
  40. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/null_decompressor.rb +27 -0
  41. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/null_input_stream.rb +10 -0
  42. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/output_stream.rb +190 -0
  43. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/pass_thru_compressor.rb +23 -0
  44. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/pass_thru_decompressor.rb +41 -0
  45. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/streamable_directory.rb +15 -0
  46. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/streamable_stream.rb +56 -0
  47. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip/version.rb +3 -0
  48. data/lib/hotplate/gems/rubyzip-1.1.7/lib/zip.rb +63 -0
  49. data/lib/hotplate/gems/rubyzip-1.1.7/samples/example.rb +91 -0
  50. data/lib/hotplate/gems/rubyzip-1.1.7/samples/example_filesystem.rb +33 -0
  51. data/lib/hotplate/gems/rubyzip-1.1.7/samples/example_recursive.rb +48 -0
  52. data/lib/hotplate/gems/rubyzip-1.1.7/samples/gtkRubyzip.rb +86 -0
  53. data/lib/hotplate/gems/rubyzip-1.1.7/samples/qtzip.rb +101 -0
  54. data/lib/hotplate/gems/rubyzip-1.1.7/samples/write_simple.rb +13 -0
  55. data/lib/hotplate/gems/rubyzip-1.1.7/samples/zipfind.rb +74 -0
  56. data/lib/hotplate/gems/rubyzip-1.1.7/test/basic_zip_file_test.rb +64 -0
  57. data/lib/hotplate/gems/rubyzip-1.1.7/test/central_directory_entry_test.rb +73 -0
  58. data/lib/hotplate/gems/rubyzip-1.1.7/test/central_directory_test.rb +104 -0
  59. data/lib/hotplate/gems/rubyzip-1.1.7/test/crypto/null_encryption_test.rb +53 -0
  60. data/lib/hotplate/gems/rubyzip-1.1.7/test/crypto/traditional_encryption_test.rb +80 -0
  61. data/lib/hotplate/gems/rubyzip-1.1.7/test/data/WarnInvalidDate.zip +0 -0
  62. data/lib/hotplate/gems/rubyzip-1.1.7/test/data/file1.txt +46 -0
  63. data/lib/hotplate/gems/rubyzip-1.1.7/test/data/file1.txt.deflatedData +0 -0
  64. data/lib/hotplate/gems/rubyzip-1.1.7/test/data/file2.txt +1504 -0
  65. data/lib/hotplate/gems/rubyzip-1.1.7/test/data/globTest/foo/bar/baz/foo.txt +0 -0
  66. data/lib/hotplate/gems/rubyzip-1.1.7/test/data/globTest/foo.txt +0 -0
  67. data/lib/hotplate/gems/rubyzip-1.1.7/test/data/globTest/food.txt +0 -0
  68. data/lib/hotplate/gems/rubyzip-1.1.7/test/data/globTest.zip +0 -0
  69. data/lib/hotplate/gems/rubyzip-1.1.7/test/data/mimetype +1 -0
  70. data/lib/hotplate/gems/rubyzip-1.1.7/test/data/notzippedruby.rb +7 -0
  71. data/lib/hotplate/gems/rubyzip-1.1.7/test/data/ntfs.zip +0 -0
  72. data/lib/hotplate/gems/rubyzip-1.1.7/test/data/rubycode.zip +0 -0
  73. data/lib/hotplate/gems/rubyzip-1.1.7/test/data/rubycode2.zip +0 -0
  74. data/lib/hotplate/gems/rubyzip-1.1.7/test/data/testDirectory.bin +0 -0
  75. data/lib/hotplate/gems/rubyzip-1.1.7/test/data/zip64-sample.zip +0 -0
  76. data/lib/hotplate/gems/rubyzip-1.1.7/test/data/zipWithDirs.zip +0 -0
  77. data/lib/hotplate/gems/rubyzip-1.1.7/test/data/zipWithEncryption.zip +0 -0
  78. data/lib/hotplate/gems/rubyzip-1.1.7/test/deflater_test.rb +67 -0
  79. data/lib/hotplate/gems/rubyzip-1.1.7/test/encryption_test.rb +42 -0
  80. data/lib/hotplate/gems/rubyzip-1.1.7/test/entry_set_test.rb +138 -0
  81. data/lib/hotplate/gems/rubyzip-1.1.7/test/entry_test.rb +165 -0
  82. data/lib/hotplate/gems/rubyzip-1.1.7/test/errors_test.rb +36 -0
  83. data/lib/hotplate/gems/rubyzip-1.1.7/test/extra_field_test.rb +78 -0
  84. data/lib/hotplate/gems/rubyzip-1.1.7/test/file_extract_directory_test.rb +56 -0
  85. data/lib/hotplate/gems/rubyzip-1.1.7/test/file_extract_test.rb +90 -0
  86. data/lib/hotplate/gems/rubyzip-1.1.7/test/file_split_test.rb +60 -0
  87. data/lib/hotplate/gems/rubyzip-1.1.7/test/file_test.rb +559 -0
  88. data/lib/hotplate/gems/rubyzip-1.1.7/test/filesystem/dir_iterator_test.rb +62 -0
  89. data/lib/hotplate/gems/rubyzip-1.1.7/test/filesystem/directory_test.rb +131 -0
  90. data/lib/hotplate/gems/rubyzip-1.1.7/test/filesystem/file_mutating_test.rb +100 -0
  91. data/lib/hotplate/gems/rubyzip-1.1.7/test/filesystem/file_nonmutating_test.rb +514 -0
  92. data/lib/hotplate/gems/rubyzip-1.1.7/test/filesystem/file_stat_test.rb +66 -0
  93. data/lib/hotplate/gems/rubyzip-1.1.7/test/gentestfiles.rb +134 -0
  94. data/lib/hotplate/gems/rubyzip-1.1.7/test/inflater_test.rb +14 -0
  95. data/lib/hotplate/gems/rubyzip-1.1.7/test/input_stream_test.rb +170 -0
  96. data/lib/hotplate/gems/rubyzip-1.1.7/test/ioextras/abstract_input_stream_test.rb +103 -0
  97. data/lib/hotplate/gems/rubyzip-1.1.7/test/ioextras/abstract_output_stream_test.rb +106 -0
  98. data/lib/hotplate/gems/rubyzip-1.1.7/test/ioextras/fake_io_test.rb +18 -0
  99. data/lib/hotplate/gems/rubyzip-1.1.7/test/local_entry_test.rb +156 -0
  100. data/lib/hotplate/gems/rubyzip-1.1.7/test/output_stream_test.rb +129 -0
  101. data/lib/hotplate/gems/rubyzip-1.1.7/test/pass_thru_compressor_test.rb +31 -0
  102. data/lib/hotplate/gems/rubyzip-1.1.7/test/pass_thru_decompressor_test.rb +15 -0
  103. data/lib/hotplate/gems/rubyzip-1.1.7/test/settings_test.rb +92 -0
  104. data/lib/hotplate/gems/rubyzip-1.1.7/test/test_helper.rb +228 -0
  105. data/lib/hotplate/gems/rubyzip-1.1.7/test/unicode_file_names_and_comments_test.rb +52 -0
  106. data/lib/hotplate/gems/rubyzip-1.1.7/test/zip64_full_test.rb +53 -0
  107. data/lib/hotplate/gems/rubyzip-1.1.7/test/zip64_support_test.rb +15 -0
  108. data/lib/hotplate/java/build.gradle +38 -0
  109. data/lib/hotplate/java/gradle/wrapper/gradle-wrapper.jar +0 -0
  110. data/lib/hotplate/java/gradle/wrapper/gradle-wrapper.properties +6 -0
  111. data/lib/hotplate/java/gradlew +164 -0
  112. data/lib/hotplate/java/gradlew.bat +90 -0
  113. data/lib/hotplate/java/template.java +25 -0
  114. data/lib/hotplate/version.rb +3 -0
  115. data/lib/hotplate.rb +2 -0
  116. metadata +186 -0
@@ -0,0 +1,436 @@
1
+ module Zip
2
+ # ZipFile is modeled after java.util.zip.ZipFile from the Java SDK.
3
+ # The most important methods are those inherited from
4
+ # ZipCentralDirectory for accessing information about the entries in
5
+ # the archive and methods such as get_input_stream and
6
+ # get_output_stream for reading from and writing entries to the
7
+ # archive. The class includes a few convenience methods such as
8
+ # #extract for extracting entries to the filesystem, and #remove,
9
+ # #replace, #rename and #mkdir for making simple modifications to
10
+ # the archive.
11
+ #
12
+ # Modifications to a zip archive are not committed until #commit or
13
+ # #close is called. The method #open accepts a block following
14
+ # the pattern from File.open offering a simple way to
15
+ # automatically close the archive when the block returns.
16
+ #
17
+ # The following example opens zip archive <code>my.zip</code>
18
+ # (creating it if it doesn't exist) and adds an entry
19
+ # <code>first.txt</code> and a directory entry <code>a_dir</code>
20
+ # to it.
21
+ #
22
+ # require 'zip'
23
+ #
24
+ # Zip::File.open("my.zip", Zip::File::CREATE) {
25
+ # |zipfile|
26
+ # zipfile.get_output_stream("first.txt") { |f| f.puts "Hello from ZipFile" }
27
+ # zipfile.mkdir("a_dir")
28
+ # }
29
+ #
30
+ # The next example reopens <code>my.zip</code> writes the contents of
31
+ # <code>first.txt</code> to standard out and deletes the entry from
32
+ # the archive.
33
+ #
34
+ # require 'zip'
35
+ #
36
+ # Zip::File.open("my.zip", Zip::File::CREATE) {
37
+ # |zipfile|
38
+ # puts zipfile.read("first.txt")
39
+ # zipfile.remove("first.txt")
40
+ # }
41
+ #
42
+ # ZipFileSystem offers an alternative API that emulates ruby's
43
+ # interface for accessing the filesystem, ie. the File and Dir classes.
44
+
45
+ class File < CentralDirectory
46
+
47
+ CREATE = 1
48
+ SPLIT_SIGNATURE = 0x08074b50
49
+ ZIP64_EOCD_SIGNATURE = 0x06064b50
50
+ MAX_SEGMENT_SIZE = 3221225472
51
+ MIN_SEGMENT_SIZE = 65536
52
+ DATA_BUFFER_SIZE = 8192
53
+
54
+ attr_reader :name
55
+
56
+ # default -> false
57
+ attr_accessor :restore_ownership
58
+ # default -> false
59
+ attr_accessor :restore_permissions
60
+ # default -> true
61
+ attr_accessor :restore_times
62
+ # Returns the zip files comment, if it has one
63
+ attr_accessor :comment
64
+
65
+ # Opens a zip archive. Pass true as the second parameter to create
66
+ # a new archive if it doesn't exist already.
67
+ def initialize(file_name, create = nil, buffer = false, options = {})
68
+ super()
69
+ @name = file_name
70
+ @comment = ''
71
+ @create = create
72
+ case
73
+ when !buffer && ::File.size?(file_name)
74
+ @create = nil
75
+ @exist_file_perms = ::File.stat(file_name).mode
76
+ ::File.open(name, 'rb') do |f|
77
+ read_from_stream(f)
78
+ end
79
+ when create
80
+ @entry_set = EntrySet.new
81
+ else
82
+ raise Error, "File #{file_name} not found"
83
+ end
84
+ @stored_entries = @entry_set.dup
85
+ @stored_comment = @comment
86
+ @restore_ownership = options[:restore_ownership] || false
87
+ @restore_permissions = options[:restore_permissions] || true
88
+ @restore_times = options[:restore_times] || true
89
+ end
90
+
91
+ class << self
92
+ # Same as #new. If a block is passed the ZipFile object is passed
93
+ # to the block and is automatically closed afterwards just as with
94
+ # ruby's builtin File.open method.
95
+ def open(file_name, create = nil)
96
+ zf = ::Zip::File.new(file_name, create)
97
+ return zf unless block_given?
98
+ begin
99
+ yield zf
100
+ ensure
101
+ zf.close
102
+ end
103
+ end
104
+
105
+ # Same as #open. But outputs data to a buffer instead of a file
106
+ def add_buffer
107
+ io = ::StringIO.new('')
108
+ zf = ::Zip::File.new(io, true, true)
109
+ yield zf
110
+ zf.write_buffer(io)
111
+ end
112
+
113
+ # Like #open, but reads zip archive contents from a String or open IO
114
+ # stream, and outputs data to a buffer.
115
+ # (This can be used to extract data from a
116
+ # downloaded zip archive without first saving it to disk.)
117
+ def open_buffer(io, options = {})
118
+ unless io.is_a?(IO) || io.is_a?(String) || io.is_a?(Tempfile)
119
+ raise "Zip::File.open_buffer expects an argument of class String, IO, or Tempfile. Found: #{io.class}"
120
+ end
121
+ if io.is_a?(::String)
122
+ require 'stringio'
123
+ io = ::StringIO.new(io)
124
+ elsif io.is_a?(IO)
125
+ # https://github.com/rubyzip/rubyzip/issues/119
126
+ io.binmode
127
+ end
128
+ zf = ::Zip::File.new(io, true, true, options)
129
+ zf.read_from_stream(io)
130
+ yield zf
131
+ begin
132
+ zf.write_buffer(io)
133
+ rescue IOError => e
134
+ raise unless e.message == "not opened for writing"
135
+ end
136
+ end
137
+
138
+ # Iterates over the contents of the ZipFile. This is more efficient
139
+ # than using a ZipInputStream since this methods simply iterates
140
+ # through the entries in the central directory structure in the archive
141
+ # whereas ZipInputStream jumps through the entire archive accessing the
142
+ # local entry headers (which contain the same information as the
143
+ # central directory).
144
+ def foreach(aZipFileName, &block)
145
+ open(aZipFileName) do |zipFile|
146
+ zipFile.each(&block)
147
+ end
148
+ end
149
+
150
+ def get_segment_size_for_split(segment_size)
151
+ case
152
+ when MIN_SEGMENT_SIZE > segment_size
153
+ MIN_SEGMENT_SIZE
154
+ when MAX_SEGMENT_SIZE < segment_size
155
+ MAX_SEGMENT_SIZE
156
+ else
157
+ segment_size
158
+ end
159
+ end
160
+
161
+ def get_partial_zip_file_name(zip_file_name, partial_zip_file_name)
162
+ partial_zip_file_name = zip_file_name.sub(/#{::File.basename(zip_file_name)}\z/,
163
+ partial_zip_file_name + ::File.extname(zip_file_name)) unless partial_zip_file_name.nil?
164
+ partial_zip_file_name ||= zip_file_name
165
+ partial_zip_file_name
166
+ end
167
+
168
+ def get_segment_count_for_split(zip_file_size, segment_size)
169
+ (zip_file_size / segment_size).to_i + (zip_file_size % segment_size == 0 ? 0 : 1)
170
+ end
171
+
172
+ def put_split_signature(szip_file, segment_size)
173
+ signature_packed = [SPLIT_SIGNATURE].pack('V')
174
+ szip_file << signature_packed
175
+ segment_size - signature_packed.size
176
+ end
177
+
178
+ #
179
+ # TODO: Make the code more understandable
180
+ #
181
+ def save_splited_part(zip_file, partial_zip_file_name, zip_file_size, szip_file_index, segment_size, segment_count)
182
+ ssegment_size = zip_file_size - zip_file.pos
183
+ ssegment_size = segment_size if ssegment_size > segment_size
184
+ szip_file_name = "#{partial_zip_file_name}.#{'%03d'%(szip_file_index)}"
185
+ ::File.open(szip_file_name, 'wb') do |szip_file|
186
+ if szip_file_index == 1
187
+ ssegment_size = put_split_signature(szip_file, segment_size)
188
+ end
189
+ chunk_bytes = 0
190
+ until ssegment_size == chunk_bytes || zip_file.eof?
191
+ segment_bytes_left = ssegment_size - chunk_bytes
192
+ buffer_size = segment_bytes_left < DATA_BUFFER_SIZE ? segment_bytes_left : DATA_BUFFER_SIZE
193
+ chunk = zip_file.read(buffer_size)
194
+ chunk_bytes += buffer_size
195
+ szip_file << chunk
196
+ # Info for track splitting
197
+ yield segment_count, szip_file_index, chunk_bytes, ssegment_size if block_given?
198
+ end
199
+ end
200
+ end
201
+
202
+ # Splits an archive into parts with segment size
203
+ def split(zip_file_name, segment_size = MAX_SEGMENT_SIZE, delete_zip_file = true, partial_zip_file_name = nil)
204
+ raise Error, "File #{zip_file_name} not found" unless ::File.exist?(zip_file_name)
205
+ raise Errno::ENOENT, zip_file_name unless ::File.readable?(zip_file_name)
206
+ zip_file_size = ::File.size(zip_file_name)
207
+ segment_size = get_segment_size_for_split(segment_size)
208
+ return if zip_file_size <= segment_size
209
+ segment_count = get_segment_count_for_split(zip_file_size, segment_size)
210
+ # Checking for correct zip structure
211
+ self.open(zip_file_name) {}
212
+ partial_zip_file_name = get_partial_zip_file_name(zip_file_name, partial_zip_file_name)
213
+ szip_file_index = 0
214
+ ::File.open(zip_file_name, 'rb') do |zip_file|
215
+ until zip_file.eof?
216
+ szip_file_index += 1
217
+ save_splited_part(zip_file, partial_zip_file_name, zip_file_size, szip_file_index, segment_size, segment_count)
218
+ end
219
+ end
220
+ ::File.delete(zip_file_name) if delete_zip_file
221
+ szip_file_index
222
+ end
223
+ end
224
+
225
+
226
+ # Returns an input stream to the specified entry. If a block is passed
227
+ # the stream object is passed to the block and the stream is automatically
228
+ # closed afterwards just as with ruby's builtin File.open method.
229
+ def get_input_stream(entry, &aProc)
230
+ get_entry(entry).get_input_stream(&aProc)
231
+ end
232
+
233
+ # Returns an output stream to the specified entry. If entry is not an instance
234
+ # of Zip::Entry, a new Zip::Entry will be initialized using the arguments
235
+ # specified. If a block is passed the stream object is passed to the block and
236
+ # the stream is automatically closed afterwards just as with ruby's builtin
237
+ # File.open method.
238
+ def get_output_stream(entry, permission_int = nil, comment = nil, extra = nil, compressed_size = nil, crc = nil, compression_method = nil, size = nil, time = nil, &aProc)
239
+ new_entry =
240
+ if entry.kind_of?(Entry)
241
+ entry
242
+ else
243
+ Entry.new(@name, entry.to_s, comment, extra, compressed_size, crc, compression_method, size, time)
244
+ end
245
+ if new_entry.directory?
246
+ raise ArgumentError,
247
+ "cannot open stream to directory entry - '#{new_entry}'"
248
+ end
249
+ new_entry.unix_perms = permission_int
250
+ zip_streamable_entry = StreamableStream.new(new_entry)
251
+ @entry_set << zip_streamable_entry
252
+ zip_streamable_entry.get_output_stream(&aProc)
253
+ end
254
+
255
+ # Returns the name of the zip archive
256
+ def to_s
257
+ @name
258
+ end
259
+
260
+ # Returns a string containing the contents of the specified entry
261
+ def read(entry)
262
+ get_input_stream(entry) { |is| is.read }
263
+ end
264
+
265
+ # Convenience method for adding the contents of a file to the archive
266
+ def add(entry, src_path, &continue_on_exists_proc)
267
+ continue_on_exists_proc ||= proc { ::Zip.continue_on_exists_proc }
268
+ check_entry_exists(entry, continue_on_exists_proc, "add")
269
+ new_entry = entry.kind_of?(::Zip::Entry) ? entry : ::Zip::Entry.new(@name, entry.to_s)
270
+ new_entry.gather_fileinfo_from_srcpath(src_path)
271
+ new_entry.dirty = true
272
+ @entry_set << new_entry
273
+ end
274
+
275
+ # Removes the specified entry.
276
+ def remove(entry)
277
+ @entry_set.delete(get_entry(entry))
278
+ end
279
+
280
+ # Renames the specified entry.
281
+ def rename(entry, new_name, &continue_on_exists_proc)
282
+ foundEntry = get_entry(entry)
283
+ check_entry_exists(new_name, continue_on_exists_proc, 'rename')
284
+ @entry_set.delete(foundEntry)
285
+ foundEntry.name = new_name
286
+ @entry_set << foundEntry
287
+ end
288
+
289
+ # Replaces the specified entry with the contents of srcPath (from
290
+ # the file system).
291
+ def replace(entry, srcPath)
292
+ check_file(srcPath)
293
+ remove(entry)
294
+ add(entry, srcPath)
295
+ end
296
+
297
+ # Extracts entry to file dest_path.
298
+ def extract(entry, dest_path, &block)
299
+ block ||= proc { ::Zip.on_exists_proc }
300
+ found_entry = get_entry(entry)
301
+ found_entry.extract(dest_path, &block)
302
+ end
303
+
304
+ # Commits changes that has been made since the previous commit to
305
+ # the zip archive.
306
+ def commit
307
+ return unless commit_required?
308
+ on_success_replace do |tmp_file|
309
+ ::Zip::OutputStream.open(tmp_file) do |zos|
310
+ @entry_set.each do |e|
311
+ e.write_to_zip_output_stream(zos)
312
+ e.dirty = false
313
+ e.clean_up
314
+ end
315
+ zos.comment = comment
316
+ end
317
+ true
318
+ end
319
+ initialize(name)
320
+ end
321
+
322
+ # Write buffer write changes to buffer and return
323
+ def write_buffer(io = ::StringIO.new(''))
324
+ ::Zip::OutputStream.write_buffer(io) do |zos|
325
+ @entry_set.each { |e| e.write_to_zip_output_stream(zos) }
326
+ zos.comment = comment
327
+ end
328
+ end
329
+
330
+ # Closes the zip file committing any changes that has been made.
331
+ def close
332
+ commit
333
+ end
334
+
335
+ # Returns true if any changes has been made to this archive since
336
+ # the previous commit
337
+ def commit_required?
338
+ @entry_set.each do |e|
339
+ return true if e.dirty
340
+ end
341
+ @comment != @stored_comment || @entry_set != @stored_entries || @create == ::Zip::File::CREATE
342
+ end
343
+
344
+ # Searches for entry with the specified name. Returns nil if
345
+ # no entry is found. See also get_entry
346
+ def find_entry(entry_name)
347
+ @entry_set.find_entry(entry_name)
348
+ end
349
+
350
+ # Searches for entries given a glob
351
+ def glob(*args, &block)
352
+ @entry_set.glob(*args, &block)
353
+ end
354
+
355
+ # Searches for an entry just as find_entry, but throws Errno::ENOENT
356
+ # if no entry is found.
357
+ def get_entry(entry)
358
+ selected_entry = find_entry(entry)
359
+ unless selected_entry
360
+ raise Errno::ENOENT, entry
361
+ end
362
+ selected_entry.restore_ownership = @restore_ownership
363
+ selected_entry.restore_permissions = @restore_permissions
364
+ selected_entry.restore_times = @restore_times
365
+ selected_entry
366
+ end
367
+
368
+ # Creates a directory
369
+ def mkdir(entryName, permissionInt = 0755)
370
+ if find_entry(entryName)
371
+ raise Errno::EEXIST, "File exists - #{entryName}"
372
+ end
373
+ entryName = entryName.dup.to_s
374
+ entryName << '/' unless entryName.end_with?('/')
375
+ @entry_set << ::Zip::StreamableDirectory.new(@name, entryName, nil, permissionInt)
376
+ end
377
+
378
+ private
379
+
380
+ def is_directory(newEntry, srcPath)
381
+ srcPathIsDirectory = ::File.directory?(srcPath)
382
+ if newEntry.is_directory && !srcPathIsDirectory
383
+ raise ArgumentError,
384
+ "entry name '#{newEntry}' indicates directory entry, but "+
385
+ "'#{srcPath}' is not a directory"
386
+ elsif !newEntry.is_directory && srcPathIsDirectory
387
+ newEntry.name += "/"
388
+ end
389
+ newEntry.is_directory && srcPathIsDirectory
390
+ end
391
+
392
+ def check_entry_exists(entryName, continue_on_exists_proc, procedureName)
393
+ continue_on_exists_proc ||= proc { Zip.continue_on_exists_proc }
394
+ if @entry_set.include?(entryName)
395
+ if continue_on_exists_proc.call
396
+ remove get_entry(entryName)
397
+ else
398
+ raise ::Zip::EntryExistsError,
399
+ procedureName + " failed. Entry #{entryName} already exists"
400
+ end
401
+ end
402
+ end
403
+
404
+ def check_file(path)
405
+ unless ::File.readable?(path)
406
+ raise Errno::ENOENT, path
407
+ end
408
+ end
409
+
410
+ def on_success_replace
411
+ tmpfile = get_tempfile
412
+ tmp_filename = tmpfile.path
413
+ ObjectSpace.undefine_finalizer(tmpfile)
414
+ tmpfile.close
415
+ if yield tmp_filename
416
+ ::File.rename(tmp_filename, self.name)
417
+ if defined?(@exist_file_perms)
418
+ ::File.chmod(@exist_file_perms, self.name)
419
+ end
420
+ end
421
+ ensure
422
+ tmpfile.unlink if tmpfile
423
+ end
424
+
425
+ def get_tempfile
426
+ temp_file = Tempfile.new(::File.basename(name), ::File.dirname(name))
427
+ temp_file.binmode
428
+ temp_file
429
+ end
430
+
431
+ end
432
+ end
433
+
434
+ # Copyright (C) 2002, 2003 Thomas Sondergaard
435
+ # rubyzip is free software; you can redistribute it and/or
436
+ # modify it under the terms of the ruby license.