Kouhei Sutou
null+****@clear*****
Thu Jan 2 00:11:39 JST 2014
Kouhei Sutou 2014-01-02 00:11:39 +0900 (Thu, 02 Jan 2014) New Revision: 21965e515aeba8d1a6ed160d8805f6c950807dc2 https://github.com/ranguba/chupa-text/commit/21965e515aeba8d1a6ed160d8805f6c950807dc2 Message: Add gzip decomposer Added files: lib/chupa-text/plugin/decomposer/gzip.rb test/decomposer/test-gzip.rb test/fixture/gzip/hello.tar.gz test/fixture/gzip/hello.tgz test/fixture/gzip/hello.txt.gz Added: lib/chupa-text/plugin/decomposer/gzip.rb (+51 -0) 100644 =================================================================== --- /dev/null +++ lib/chupa-text/plugin/decomposer/gzip.rb 2014-01-02 00:11:39 +0900 (ec12f50) @@ -0,0 +1,51 @@ +# Copyright (C) 2013 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 "zlib" + +require "chupa-text" + +module ChupaText + class GzipDecomposer < Decomposer + registry.register(self) + + TARGET_EXTENSIONS = ["gz", "tgz"] + TARGET_CONTENT_TYPES = [ + "application/gzip", + "application/x-gzip", + "application/x-gtar-compressed", + ] + def target?(data) + TARGET_EXTENSIONS.include?(data.extension) or + TARGET_CONTENT_TYPES.include?(data.content_type) + end + + def decompose(data) + reader = Zlib::GzipReader.new(StringIO.new(data.body)) + extracted = Data.new + case data.extension + when "gz" + extracted.path = data.path.to_s.gsub(/\.gz\z/i, "") + when "tgz" + extracted.path = data.path.to_s.gsub(/\.tgz\z/i, ".tar") + end + extracted.body = reader.read + extracted.source = data + yield(extracted) + end + end +end Added: test/decomposer/test-gzip.rb (+116 -0) 100644 =================================================================== --- /dev/null +++ test/decomposer/test-gzip.rb 2014-01-02 00:11:39 +0900 (fb0964c) @@ -0,0 +1,116 @@ +# Copyright (C) 2013 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 TestGzipDecomposer < Test::Unit::TestCase + include Helper + + def setup + @decomposer = ChupaText::GzipDecomposer.new + end + + private + def fixture_path(*components) + super("gzip", *components) + end + + sub_test_case("decompose") do + def decompose(data) + decomposed = [] + @decomposer.decompose(data) do |decomposed_data| + decomposed << decomposed_data + end + decomposed + end + + sub_test_case("gz") do + def setup + super + @data = ChupaText::Data.new + @data.path = fixture_path("hello.txt.gz") + end + + def test_path + assert_equal([fixture_path("hello.txt")], + decompose(@data).collect(&:path)) + end + + def test_body + assert_equal(["Hello\n"], + decompose(@data).collect(&:body)) + end + + def test_source + assert_equal([@data], + decompose(@data).collect(&:source)) + end + end + + sub_test_case("tar.gz") do + def setup + super + @data = ChupaText::Data.new + @data.path = fixture_path("hello.tar.gz") + end + + def test_path + assert_equal([fixture_path("hello.tar")], + decompose(@data).collect(&:path)) + end + + def test_body + tar_magic = "ustar" + magics = decompose(@data).collect do |decomposed| + decomposed.body[257, tar_magic.bytesize] + end + assert_equal([tar_magic], + magics) + end + + def test_source + assert_equal([@data], + decompose(@data).collect(&:source)) + end + end + + + sub_test_case("tgz") do + def setup + super + @data = ChupaText::Data.new + @data.path = fixture_path("hello.tgz") + end + + def test_path + assert_equal([fixture_path("hello.tar")], + decompose(@data).collect(&:path)) + end + + def test_body + tar_magic = "ustar" + magics = decompose(@data).collect do |decomposed| + decomposed.body[257, tar_magic.bytesize] + end + assert_equal([tar_magic], + magics) + end + + def test_source + assert_equal([@data], + decompose(@data).collect(&:source)) + end + end + end +end Added: test/fixture/gzip/hello.tar.gz (+0 -0) 100644 =================================================================== (Binary files differ) Added: test/fixture/gzip/hello.tgz (+0 -0) 100644 =================================================================== (Binary files differ) Added: test/fixture/gzip/hello.txt.gz (+0 -0) 100644 =================================================================== (Binary files differ) -------------- next part -------------- HTML����������������������������... ダウンロード