Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zip::File.add_stored() to add uncompressed files. #366

Merged
merged 1 commit into from Sep 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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