Commit 3e20bb00 authored by charlie-ablett's avatar charlie-ablett

Zip and unzip utility steps

parent 9886aebc
......@@ -48,6 +48,7 @@ module InkStep
def files_in_working_directory
Dir.chdir(working_directory) do
Dir.glob(File.join("**", "*.*"))
end
end
......
module InkStep::Mixins
# From rubyzip readme, accessed 24 July 2017
# This is a simple example which uses rubyzip to
# recursively generate a zip file from the contents of
# a specified directory. The directory itself is not
# included in the archive, rather just its contents.
#
# Usage:
# directory_to_zip = "/tmp/input"
# 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(input_dir, output_file)
@input_dir = input_dir
@output_file = output_file
end
# Zip the input directory.
def write
entries = Dir.entries(@input_dir) - %w(. ..)
::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.
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}"
if File.directory? disk_file_path
recursively_deflate_directory(disk_file_path, io, zip_file_path)
else
put_into_archive(disk_file_path, io, zip_file_path)
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.write(File.open(disk_file_path, 'rb').read)
end
end
end
end
\ No newline at end of file
require 'ink_step/base'
require 'zip'
module InkStep::UtilitySteps
class UnzipStep < ::InkStep::Base
def perform_step(options: {})
source_file_path = find_source_file(regex: /\.#{parameter(options, :zip_file_extension)}$/)
unzip_file(File.join(working_directory, source_file_path))
success!
end
def self.description
"Unzips target archive"
end
def version
InkStep::VERSION
end
def required_parameters
# e.g. [:foo, :bar]
[:zip_file_extension]
end
def accepted_parameters
{zip_file_extension: "The extension of the archive file to be unzipped"}
end
def default_parameter_values
{zip_file_extension: "zip"}
end
protected
def unzip_file(input_file_path)
log_as_step "Unzipping archive..."
Zip::File.open(input_file_path) do |archive|
archive.each do |f|
@archive_name = f.name unless @archive_name
path = File.join(unzip_directory, f.name)
FileUtils.mkdir_p(File.dirname(path)) unless File.exists?(File.dirname(path))
f.extract(path)
end
end
end
def unzip_directory
File.join(working_directory, @archive_name)
end
end
end
\ No newline at end of file
require 'ink_step/base'
require 'ink_step/mixins/shell_methods'
module InkStep::UtilitySteps
class ZipStep < ::InkStep::Base
def perform_step(options: {})
# source_file_path = find_source_file(regex: /\.#{parameter(options, :zip_file_name)}$/)
# unzip_file(File.join(working_directory, source_file_path))
success!
end
def self.description
"Zips all files in the working directory into an archive"
end
def version
InkStep::VERSION
end
def required_parameters
# e.g. [:foo, :bar]
[:zip_file_name]
end
def accepted_parameters
# e.g. {foo: "For setting the grobblegronx measure", bar: "Can be X, Y or Z"}
{zip_file_name: "The name of the archive file (.zip will be added)"}
end
def default_parameter_values
# e.g. {foo: 1, bar: nil}
{zip_file_name: "archive.zip"}
end
protected
def zip_file(directory, archive_file_name)
log_as_step "Zipping archive..."
zipper = ::InkStep::Mixins::ZipGenerator.new(directory, archive_file_name)
zipper.write
end
end
end
\ No newline at end of file
module InkStep
VERSION = '1.0.3'
VERSION = '1.1.0'
end
\ No newline at end of file
require 'spec_helper'
require 'ink_step/utility_steps/unzip_step'
describe "#perform_step" do
let(:target_file_name) { "some_file.zip" }
let(:target_file) { File.join(Dir.pwd, "spec", "fixtures", "files", target_file_name) }
let!(:input_file_path) { File.join(temp_directory, InkStep::Base::INPUT_FILE_DIRECTORY_NAME) }
subject { InkStep::UtilitySteps::UnzipStep.new(chain_file_location: temp_directory, position: 1) }
before do
create_directory_if_needed(subject.working_directory)
create_directory_if_needed(input_file_path)
FileUtils.cp(target_file, subject.working_directory)
end
describe '#perform_step' do
specify do
subject.perform_step
expect(subject.errors).to match_array([])
end
end
end
require 'spec_helper'
require 'ink_step/utility_steps/zip_step'
require 'zip'
describe "#perform_step" do
let(:target_file_name) { "some_text.html" }
let(:target_file) { File.join(Dir.pwd, "spec", "fixtures", "files", target_file_name) }
let!(:input_file_path) { File.join(temp_directory, InkStep::Base::INPUT_FILE_DIRECTORY_NAME) }
subject { InkStep::UtilitySteps::ZipStep.new(chain_file_location: temp_directory, position: 1) }
before do
create_directory_if_needed(subject.working_directory)
create_directory_if_needed(input_file_path)
FileUtils.cp(target_file, subject.working_directory)
end
describe '#perform_step' do
specify do
subject.perform_step
expect(subject.errors).to match_array([])
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment