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:
|
# Usage:
|
||||||
# directoryToZip = "/tmp/input"
|
# directoryToZip = "/tmp/input"
|
||||||
# outputFile = "/tmp/out.zip"
|
# output_file = "/tmp/out.zip"
|
||||||
# zf = ZipFileGenerator.new(directoryToZip, outputFile)
|
# zf = ZipFileGenerator.new(directory_to_zip, output_file)
|
||||||
# zf.write()
|
# zf.write()
|
||||||
class ZipFileGenerator
|
class ZipFileGenerator
|
||||||
|
|
||||||
# Initialize with the directory to zip and the location of the output archive.
|
# Initialize with the directory to zip and the location of the output archive.
|
||||||
def initialize(inputDir, outputFile)
|
def initialize(input_dir, output_file)
|
||||||
@inputDir = inputDir
|
@input_dir = input_dir
|
||||||
@outputFile = outputFile
|
@output_file = output_file
|
||||||
end
|
end
|
||||||
|
|
||||||
# Zip the input directory.
|
# Zip the input directory.
|
||||||
def write()
|
def write
|
||||||
entries = Dir.entries(@inputDir); entries.delete("."); entries.delete("..")
|
entries = Dir.entries(@input_dir) - %w(. ..)
|
||||||
io = Zip::File.open(@outputFile, Zip::File::CREATE);
|
|
||||||
|
|
||||||
writeEntries(entries, "", io)
|
::Zip::File.open(@output_file, ::Zip::File::CREATE) do |io|
|
||||||
io.close();
|
write_entries entries, '', io
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
# A helper method to make the recursion work.
|
# A helper method to make the recursion work.
|
||||||
private
|
def write_entries(entries, path, io)
|
||||||
def writeEntries(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|
|
if File.directory? disk_file_path
|
||||||
zipFilePath = path == "" ? e : File.join(path, e)
|
recursively_deflate_directory(disk_file_path, io, zip_file_path)
|
||||||
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)
|
|
||||||
else
|
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
|
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
|
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