Fix Recursive zipping example
- Minor refactoring - Rubify Java-ish code - Fix issue with `::Zip` reference - Add test to ensure integrity
This commit is contained in:
parent
e1d5012f89
commit
34899f3a80
|
@ -7,42 +7,52 @@ require 'zip'
|
|||
#
|
||||
# Usage:
|
||||
# directoryToZip = "/tmp/input"
|
||||
# outputFile = "/tmp/out.zip"
|
||||
# zf = ZipFileGenerator.new(directoryToZip, outputFile)
|
||||
# output_file = "/tmp/out.zip"
|
||||
# zf = ZipFileGenerator.new(directory_to_zip, output_file)
|
||||
# zf.write()
|
||||
class ZipFileGenerator
|
||||
|
||||
# Initialize with the directory to zip and the location of the output archive.
|
||||
def initialize(inputDir, outputFile)
|
||||
@inputDir = inputDir
|
||||
@outputFile = outputFile
|
||||
def initialize(input_dir, output_file)
|
||||
@input_dir = input_dir
|
||||
@output_file = output_file
|
||||
end
|
||||
|
||||
# Zip the input directory.
|
||||
def write()
|
||||
entries = Dir.entries(@inputDir); entries.delete("."); entries.delete("..")
|
||||
io = Zip::File.open(@outputFile, Zip::File::CREATE);
|
||||
def write
|
||||
entries = Dir.entries(@input_dir) - %w(. ..)
|
||||
|
||||
writeEntries(entries, "", io)
|
||||
io.close();
|
||||
::Zip::File.open(@output_file, ::Zip::File::CREATE) do |io|
|
||||
write_entries entries, '', io
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# A helper method to make the recursion work.
|
||||
private
|
||||
def writeEntries(entries, path, io)
|
||||
def write_entries(entries, path, io)
|
||||
entries.each do |e|
|
||||
zip_file_path = path == '' ? e : File.join(path, e)
|
||||
disk_file_path = File.join(@input_dir, zip_file_path)
|
||||
puts "Deflating #{disk_file_path}"
|
||||
|
||||
entries.each { |e|
|
||||
zipFilePath = path == "" ? e : File.join(path, e)
|
||||
diskFilePath = File.join(@inputDir, zipFilePath)
|
||||
puts "Deflating " + diskFilePath
|
||||
if File.directory?(diskFilePath)
|
||||
io.mkdir(zipFilePath)
|
||||
subdir =Dir.entries(diskFilePath); subdir.delete("."); subdir.delete("..")
|
||||
writeEntries(subdir, zipFilePath, io)
|
||||
if File.directory? disk_file_path
|
||||
recursively_deflate_directory(disk_file_path, io, zip_file_path)
|
||||
else
|
||||
io.get_output_stream(zipFilePath) { |f| f.puts(File.open(diskFilePath, "rb").read())}
|
||||
put_into_archive(disk_file_path, io, zip_file_path)
|
||||
end
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
def recursively_deflate_directory(disk_file_path, io, zip_file_path)
|
||||
io.mkdir zip_file_path
|
||||
subdir = Dir.entries(disk_file_path) - %w(. ..)
|
||||
write_entries subdir, zip_file_path, io
|
||||
end
|
||||
|
||||
def put_into_archive(disk_file_path, io, zip_file_path)
|
||||
io.get_output_stream(zip_file_path) do |f|
|
||||
f.puts(File.open(disk_file_path, 'rb').read)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,37 @@
|
|||
require 'test_helper'
|
||||
require 'fileutils'
|
||||
require_relative '../../samples/example_recursive'
|
||||
|
||||
class ExampleRecursiveTest < MiniTest::Test
|
||||
DIRECTORY_TO_ZIP = 'test/data/globTest'
|
||||
OUTPUT_DIRECTORY = 'test/data/example_recursive.zip'
|
||||
TEMP_DIRECTORY = 'test/data/tmp'
|
||||
|
||||
def setup
|
||||
@generator = ::ZipFileGenerator.new(DIRECTORY_TO_ZIP, OUTPUT_DIRECTORY)
|
||||
end
|
||||
|
||||
def teardown
|
||||
FileUtils.rm_rf TEMP_DIRECTORY
|
||||
FileUtils.rm_f OUTPUT_DIRECTORY
|
||||
end
|
||||
|
||||
def test_write
|
||||
@generator.write
|
||||
unzip
|
||||
assert_equal Dir.entries(DIRECTORY_TO_ZIP).sort, Dir.entries(TEMP_DIRECTORY).sort
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def unzip(file = OUTPUT_DIRECTORY)
|
||||
Zip::File.open(file) do |zip_file|
|
||||
zip_file.each do |f|
|
||||
file_path = File.join(TEMP_DIRECTORY, f.name)
|
||||
FileUtils.mkdir_p(File.dirname(file_path))
|
||||
|
||||
zip_file.extract(f, file_path) unless File.exist?(file_path)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue