Skip to content

Commit

Permalink
Zip::File.add_stored() to add uncompressed files.
Browse files Browse the repository at this point in the history
Adding uncompressed files to a zip archive can be overly complex, so
this convenience method makes it easier.
  • Loading branch information
hainesr committed Sep 14, 2019
1 parent 09bb946 commit ecb2776
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/zip/file.rb
Expand Up @@ -287,6 +287,13 @@ def add(entry, src_path, &continue_on_exists_proc)
@entry_set << new_entry
end

# Convenience method for adding the contents of a file to the archive
# in Stored format (uncompressed)
def add_stored(entry, src_path, &continue_on_exists_proc)
entry = ::Zip::Entry.new(@name, entry.to_s, nil, nil, nil, nil, ::Zip::Entry::STORED)
add(entry, src_path, &continue_on_exists_proc)
end

# Removes the specified entry.
def remove(entry)
@entry_set.delete(get_entry(entry))
Expand Down
19 changes: 19 additions & 0 deletions test/file_test.rb
Expand Up @@ -204,6 +204,25 @@ def test_add
zfRead.get_input_stream(entryName) { |zis| zis.read })
end

def test_add_stored
srcFile = 'test/data/file2.txt'
entryName = 'newEntryName.rb'
assert(::File.exist?(srcFile))
zf = ::Zip::File.new(EMPTY_FILENAME, ::Zip::File::CREATE)
zf.add_stored(entryName, srcFile)
zf.close

zfRead = ::Zip::File.new(EMPTY_FILENAME)
entry = zfRead.entries.first
assert_equal('', zfRead.comment)
assert_equal(1, zfRead.entries.length)
assert_equal(entryName, entry.name)
assert_equal(entry.size, entry.compressed_size)
assert_equal(::Zip::Entry::STORED, entry.compression_method)
AssertEntry.assert_contents(srcFile,
zfRead.get_input_stream(entryName) { |zis| zis.read })
end

def test_recover_permissions_after_add_files_to_archive
srcZip = TEST_ZIP.zip_name
::File.chmod(0o664, srcZip)
Expand Down

1 comment on commit ecb2776

@JangJungAn
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

플리즈

Please sign in to comment.