Kouhei Sutou
null+****@clear*****
Tue Jul 11 11:59:18 JST 2017
Kouhei Sutou 2017-07-11 11:59:18 +0900 (Tue, 11 Jul 2017) New Revision: b599c4e8d2b0e84a77864929b03d3cbd3f42e8e1 https://github.com/ranguba/chupa-text/commit/b599c4e8d2b0e84a77864929b03d3cbd3f42e8e1 Message: Support zip Added files: lib/chupa-text/decomposers/zip.rb test/decomposers/test-zip.rb test/fixture/zip/hello.zip test/fixture/zip/password.zip Modified files: chupa-text.gemspec data/mime-types.conf Modified: chupa-text.gemspec (+2 -0) =================================================================== --- chupa-text.gemspec 2017-07-11 11:32:24 +0900 (4759dd8) +++ chupa-text.gemspec 2017-07-11 11:59:18 +0900 (d3da753) @@ -50,6 +50,8 @@ Gem::Specification.new do |spec| spec.executables = Dir.glob("*") end + spec.add_runtime_dependency("archive-zip") + spec.add_development_dependency("bundler") spec.add_development_dependency("rake") spec.add_development_dependency("test-unit") Modified: data/mime-types.conf (+2 -0) =================================================================== --- data/mime-types.conf 2017-07-11 11:32:24 +0900 (b3b4533) +++ data/mime-types.conf 2017-07-11 11:59:18 +0900 (726474c) @@ -7,6 +7,8 @@ mime_types["tgz"] = "application/x-gtar-compressed" mime_types["tar"] = "application/x-tar" +mime_types["zip"] = "application/zip" + mime_types["htm"] = "text/html" mime_types["html"] = "text/html" mime_types["xhtml"] = "application/xhtml+xml" Added: lib/chupa-text/decomposers/zip.rb (+58 -0) 100644 =================================================================== --- /dev/null +++ lib/chupa-text/decomposers/zip.rb 2017-07-11 11:59:18 +0900 (bef8a02) @@ -0,0 +1,58 @@ +# Copyright (C) 2017 Kouhei Sutou <kou �� clear-code.com> +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +require "stringio" +require "tmpdir" + +require "archive/zip" + +module ChupaText + module Decomposers + class Zip < Decomposer + registry.register("zip", self) + + def target?(data) + return true if data.extension == "zip" + return true if data.mime_type == "application/zip" + + false + end + + def decompose(data) + Archive::Zip.open(StringIO.new(data.body)) do |zip| + zip.each do |entry| + next unless entry.file? + + case entry.encryption_codec + when Archive::Zip::Codec::NullEncryption + else + # TODO + # entry.password = ... + raise EncryptedError.new(data) + end + entry_uri = data.uri.dup + base_path = entry_uri.path.gsub(/\.zip\z/i, "") + entry_uri.path = "#{base_path}/#{entry.zip_path}" + entry_data = VirtualFileData.new(entry_uri, + entry.file_data, + source_data: data) + yield(entry_data) + end + end + end + end + end +end Added: test/decomposers/test-zip.rb (+100 -0) 100644 =================================================================== --- /dev/null +++ test/decomposers/test-zip.rb 2017-07-11 11:59:18 +0900 (b9510c4) @@ -0,0 +1,100 @@ +# Copyright (C) 2017 Kouhei Sutou <kou �� clear-code.com> +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +class TestDecomposersZip < Test::Unit::TestCase + include Helper + + def setup + @decomposer = ChupaText::Decomposers::Zip.new({}) + end + + private + def fixture_path(*components) + super("zip", *components) + end + + sub_test_case("decompose") do + def decompose(data_path) + data = ChupaText::InputData.new(data_path) + decomposed = [] + @decomposer.decompose(data) do |decomposed_data| + decomposed << { + :uri => decomposed_data.uri.to_s, + :body => decomposed_data.body, + :source => decomposed_data.source.uri.to_s, + } + end + decomposed + end + + test("multiple") do + data_path = Pathname.new(fixture_path("hello.zip")) + base_path = data_path.sub_ext("") + assert_equal([ + { + :uri => "file:#{base_path}/hello.txt", + :body => "Hello!\n", + :source => "file:#{data_path}", + }, + { + :uri => "file:#{base_path}/hello.csv", + :body => "Hello,World\n", + :source => "file:#{data_path}", + }, + { + :uri => "file:#{base_path}/hello/world.txt", + :body => "World!\n", + :source => "file:#{data_path}", + }, + ], + decompose(data_path)) + end + + sub_test_case("encrypted") do + test("without password") do + data_path = Pathname.new(fixture_path("password.zip")) + data = ChupaText::InputData.new(data_path) + assert_raise(ChupaText::EncryptedError.new(data)) do + @decomposer.decompose(data) + end + end + + test("with password") do + omit("password is 'password'") + data_path = Pathname.new(fixture_path("password.zip")) + base_path = data_path.sub_ext("") + assert_equal([ + { + :uri => "file:#{base_path}/hello.txt", + :body => "Hello!\n", + :source => "file:#{data_path}", + }, + { + :uri => "file:#{base_path}/hello.csv", + :body => "Hello,World\n", + :source => "file:#{data_path}", + }, + { + :uri => "file:#{base_path}/hello/world.txt", + :body => "World!\n", + :source => "file:#{data_path}", + }, + ], + decompose(data_path)) + end + end + end +end Added: test/fixture/zip/hello.zip (+0 -0) 100644 =================================================================== (Binary files differ) Added: test/fixture/zip/password.zip (+0 -0) 100644 =================================================================== (Binary files differ) -------------- next part -------------- HTML����������������������������...Download