underlog-unpacker 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ == 1.0.0 / 2009-04-20
2
+
3
+ * 1 major enhancement
4
+ * Birthday!
@@ -0,0 +1,66 @@
1
+ # unpacker
2
+ by Petyo Ivanov
3
+
4
+ ## DESCRIPTION
5
+
6
+ Small library for unpacking various archives, extracted from Beanstalk (http://beanstalkapp.com) (and rewritten, in fact).
7
+
8
+ ## FEATURES/PROBLEMS
9
+
10
+ None so far :). Won't run on Windows, haven't tested.
11
+
12
+ ## SAMPLE USAGE
13
+
14
+ To test archives do:
15
+
16
+ Unpacker.valid? 'path/to/file.zip'
17
+
18
+ To extract files do:
19
+
20
+ Unpacker.unpack('path/to/myfile.zip') do |directory_that_contains_extracted_files|
21
+
22
+ end
23
+
24
+ In case the archive is not supported (currently working with zip, bzip, gzip, tar and rar) you may get UnrecognizedArchiveError in both methods.
25
+
26
+ The gem takes care to remove any temporary extractings after the block is executed; (uses /tmp/ directory).
27
+
28
+ When working with .gz files (single archive), the extracted file is always called **gz-contents**.
29
+
30
+ ## REQUIREMENTS
31
+
32
+ Relies on system calls, so you should have
33
+ - unzip
34
+ - unrar
35
+ - tar
36
+ - gunzip
37
+ - bunzip
38
+
39
+ In path.
40
+
41
+ ## INSTALL
42
+
43
+ `sudo gem install underlog-unpacker`
44
+
45
+ ## LICENSE
46
+
47
+ (The MIT License)
48
+
49
+ Permission is hereby granted, free of charge, to any person obtaining
50
+ a copy of this software and associated documentation files (the
51
+ 'Software'), to deal in the Software without restriction, including
52
+ without limitation the rights to use, copy, modify, merge, publish,
53
+ distribute, sublicense, and/or sell copies of the Software, and to
54
+ permit persons to whom the Software is furnished to do so, subject to
55
+ the following conditions:
56
+
57
+ The above copyright notice and this permission notice shall be
58
+ included in all copies or substantial portions of the Software.
59
+
60
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
61
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
62
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
63
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
64
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
65
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
66
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,48 @@
1
+ # Look in the tasks/setup.rb file for the various options that can be
2
+ # configured in this Rakefile. The .rake files in the tasks directory
3
+ # are where the options are used.
4
+
5
+ require 'rake/testtask'
6
+ require 'rake/rdoctask'
7
+ require 'rcov/rcovtask'
8
+
9
+ begin
10
+ require 'bones'
11
+ Bones.setup
12
+ rescue LoadError
13
+ begin
14
+ load 'tasks/setup.rb'
15
+ rescue LoadError
16
+ raise RuntimeError, '### please install the "bones" gem ###'
17
+ end
18
+ end
19
+
20
+ ensure_in_path 'lib'
21
+ require 'unpacker'
22
+
23
+ task :default => :test
24
+
25
+ desc 'Test the unpacker gem.'
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.pattern = 'test/**/*_test.rb'
29
+ t.verbose = true
30
+ end
31
+
32
+ desc 'Performs code coverage on the unpacker gem.'
33
+ Rcov::RcovTask.new do |t|
34
+ t.libs << "test"
35
+ t.test_files = FileList['test/*_test.rb']
36
+ t.verbose = true
37
+ end
38
+
39
+ PROJ.name = 'unpacker'
40
+ PROJ.authors = 'Petyo Ivanov'
41
+ PROJ.email = 'underlog@gmail.com'
42
+ PROJ.url = 'http://github.com/underlog/unpacker'
43
+ PROJ.version = Unpacker::VERSION
44
+ PROJ.rubyforge.name = 'unpacker'
45
+
46
+ PROJ.spec.opts << '--color'
47
+
48
+ # EOF
@@ -0,0 +1,103 @@
1
+ require 'fileutils'
2
+ require 'tmpdir'
3
+ require 'digest/sha1'
4
+
5
+ module Unpacker
6
+
7
+ class UnrecognizedArchiveError < StandardError; end
8
+ class UnpackedFailedError < StandardError; end
9
+
10
+
11
+ def self.unpack(file, tmpdir = "/tmp", &block)
12
+ Dir.mktmpdir 'unpacker' do |dir|
13
+ cmd = command_by_file_ext(file)% [file, dir]
14
+ system("#{cmd} 1>/dev/null") or raise UnrecognizedArchiveError($?)
15
+ block.call(Dir.new(dir))
16
+ end
17
+ end
18
+
19
+ def self.valid?(file)
20
+ cmd = test_cmd_by_file_ext(file)% file
21
+ system("#{cmd} 1>/dev/null 2>/dev/null")
22
+ rescue UnrecognizedArchiveError
23
+ false
24
+ end
25
+
26
+ # :stopdoc:
27
+ VERSION = '1.0.0'
28
+ LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
29
+ PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
30
+ # :startdoc:
31
+
32
+ # Returns the version string for the library.
33
+ #
34
+ def self.version
35
+ VERSION
36
+ end
37
+
38
+ # Returns the library path for the module. If any arguments are given,
39
+ # they will be joined to the end of the libray path using
40
+ # <tt>File.join</tt>.
41
+ #
42
+ def self.libpath( *args )
43
+ args.empty? ? LIBPATH : ::File.join(LIBPATH, args.flatten)
44
+ end
45
+
46
+ # Returns the lpath for the module. If any arguments are given,
47
+ # they will be joined to the end of the path using
48
+ # <tt>File.join</tt>.
49
+ #
50
+ def self.path( *args )
51
+ args.empty? ? PATH : ::File.join(PATH, args.flatten)
52
+ end
53
+
54
+ # Utility method used to require all files ending in .rb that lie in the
55
+ # directory below this file that has the same name as the filename passed
56
+ # in. Optionally, a specific _directory_ name can be passed in such that
57
+ # the _filename_ does not have to be equivalent to the directory.
58
+ #
59
+ def self.require_all_libs_relative_to( fname, dir = nil )
60
+ dir ||= ::File.basename(fname, '.*')
61
+ search_me = ::File.expand_path(
62
+ ::File.join(::File.dirname(fname), dir, '**', '*.rb'))
63
+
64
+ Dir.glob(search_me).sort.each {|rb| require rb}
65
+ end
66
+
67
+ private
68
+
69
+ def self.test_cmd_by_file_ext(file_name)
70
+ case file_name
71
+ when /rar$/
72
+ 'rar t "%s"'
73
+ when /(tar|tgz|tar\.gz|tar\.bz|tbz)$/
74
+ 'tar tf "%s"'
75
+ when /zip$/
76
+ 'zip -T "%s"'
77
+ when /gz$/
78
+ 'gunzip -t "%s"'
79
+ else
80
+ raise UnrecognizedArchiveError
81
+ end
82
+ end
83
+
84
+ def self.command_by_file_ext(file_name)
85
+ case file_name
86
+ when /rar$/
87
+ 'unrar x -y "%s" "%s"'
88
+ when /(tar|tgz|tar\.gz|tar\.bz|tbz)$/
89
+ 'tar xf "%s" --directory "%s"'
90
+ when /zip$/
91
+ 'unzip "%s" -d "%s"'
92
+ when /gz$/
93
+ 'gunzip -c "%s" > "%s/gz-contents"'
94
+ else
95
+ raise UnrecognizedArchiveError
96
+ end
97
+ end
98
+
99
+ end # module Unpacker
100
+
101
+ Unpacker.require_all_libs_relative_to(__FILE__)
102
+
103
+ # EOF
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,116 @@
1
+ Cras eget augue.
2
+
3
+
4
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit.
5
+ Duis congue urna ac lectus.
6
+ Fusce arcu est, sodales nec, convallis sit amet, ultricies sed, ante.
7
+ In cursus odio.
8
+ Duis congue urna ac lectus.
9
+ Donec condimentum velit eget sapien.
10
+ Vestibulum porttitor, enim nec commodo lacinia, nibh orci pulvinar lorem, sit amet rutrum massa ante laoreet mi.
11
+ In cursus odio.
12
+ Praesent in tortor sed purus ornare pellentesque.
13
+ Vivamus mattis nisi sed ipsum.
14
+ Curabitur bibendum tincidunt lorem.
15
+ In risus nisl, vulputate eget, tristique vitae, aliquet eu, ipsum.
16
+ Curabitur bibendum tincidunt lorem.
17
+ Curabitur tortor.
18
+ Donec condimentum velit eget sapien.
19
+ Curabitur tortor.
20
+ In id mi.
21
+ Curabitur tortor.
22
+ Donec condimentum velit eget sapien.
23
+ Duis a turpis.
24
+ Curabitur tortor.
25
+
26
+
27
+ Mauris tempus velit ut metus.
28
+ Fusce libero magna, mattis vel, porttitor vitae, ornare et, metus.
29
+ Pellentesque orci.
30
+ Maecenas blandit neque.
31
+ In cursus odio.
32
+ Duis laoreet egestas nibh.
33
+ Nulla adipiscing, libero quis faucibus interdum, lacus enim viverra purus, quis feugiat magna justo sed lorem.
34
+ Fusce libero magna, mattis vel, porttitor vitae, ornare et, metus.
35
+ Phasellus purus dolor, tempor non, tristique sit amet, porttitor porta, velit.
36
+ Sed faucibus tempus eros.
37
+
38
+
39
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit.
40
+ Duis laoreet egestas nibh.
41
+ Maecenas quis nulla eget nunc volutpat hendrerit.
42
+ Etiam velit.
43
+ Integer dignissim augue at velit.
44
+ In id mi.
45
+ Donec velit diam, laoreet gravida, pretium ac, tristique quis, lacus.
46
+ Suspendisse eleifend pretium massa.
47
+ Maecenas quis nulla eget nunc volutpat hendrerit.
48
+ In id mi.
49
+ Cras pulvinar.
50
+ Vestibulum at tellus sed augue rutrum dictum.
51
+ Cras eget augue.
52
+ Donec rhoncus.
53
+ Fusce libero magna, mattis vel, porttitor vitae, ornare et, metus.
54
+ Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
55
+ Duis congue urna ac lectus.
56
+ Nunc ut purus.
57
+ Nunc ut purus.
58
+ Proin sit amet augue ut arcu accumsan mollis.
59
+ Quisque eros felis, lacinia non, tempus et, vehicula eu, sem.
60
+ Donec condimentum velit eget sapien.
61
+ Fusce libero magna, mattis vel, porttitor vitae, ornare et, metus.
62
+ Fusce gravida.
63
+
64
+
65
+ Mauris tempus velit ut metus.
66
+ Donec condimentum velit eget sapien.
67
+ In risus nisl, vulputate eget, tristique vitae, aliquet eu, ipsum.
68
+ Vestibulum porttitor, enim nec commodo lacinia, nibh orci pulvinar lorem, sit amet rutrum massa ante laoreet mi.
69
+ Praesent in tortor sed purus ornare pellentesque.
70
+
71
+
72
+ Mauris tempus velit ut metus.
73
+ Praesent vestibulum, arcu et semper fringilla, magna urna sodales lacus, vel aliquet turpis tortor ac libero.
74
+ Ut commodo, nisl a lobortis dictum, quam nisi pharetra dolor, quis ullamcorper orci tortor posuere felis.
75
+ Maecenas et lorem.
76
+ Cras felis eros, placerat eget, adipiscing vitae, sodales quis, dolor.
77
+ Cras eget augue.
78
+ Etiam et neque.
79
+
80
+
81
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit.
82
+ Praesent vel erat at mi vehicula elementum.
83
+ Vivamus tincidunt felis ut libero.
84
+ In vestibulum mauris eu sapien.
85
+ Vivamus tincidunt felis ut libero.
86
+ Maecenas blandit neque.
87
+ Duis a turpis.
88
+ Phasellus est quam, faucibus vitae, euismod feugiat, ultrices et, quam.
89
+ Donec consectetur, ante in volutpat tristique, nulla metus ornare urna, vestibulum ullamcorper dui nulla fringilla eros.
90
+ Cras a ligula at diam malesuada tincidunt.
91
+ Vestibulum porttitor, enim nec commodo lacinia, nibh orci pulvinar lorem, sit amet rutrum massa ante laoreet mi.
92
+ Vivamus varius dui ac leo.
93
+ Etiam et neque.
94
+ Duis a turpis.
95
+ Pellentesque orci.
96
+ Donec condimentum velit eget sapien.
97
+ Vivamus dui orci, volutpat ac, blandit id, commodo quis, massa.
98
+ Suspendisse in sem at enim laoreet placerat.
99
+ Mauris dapibus nulla vitae tellus.
100
+ Nunc ut purus.
101
+
102
+
103
+ Etiam elementum.
104
+ Suspendisse eleifend pretium massa.
105
+ Nullam quis nulla.
106
+ Etiam velit.
107
+ Praesent vel erat at mi vehicula elementum.
108
+ In dapibus augue quis leo.
109
+ Nulla adipiscing, libero quis faucibus interdum, lacus enim viverra purus, quis feugiat magna justo sed lorem.
110
+ Curabitur bibendum tincidunt lorem.
111
+
112
+
113
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit.
114
+ Donec cursus, velit vel mollis ultrices, augue dui ornare ipsum, at ultricies mi leo in tellus.
115
+ Duis placerat purus sed dui.
116
+ Curabitur tortor.
@@ -0,0 +1,14 @@
1
+ $: << File.join(File.dirname(__FILE__), '..', 'lib')
2
+ $: << File.join(File.dirname(__FILE__))
3
+
4
+ require 'rubygems'
5
+ require 'test/unit'
6
+ require 'unpacker'
7
+
8
+ class Test::Unit::TestCase
9
+
10
+ def fixture_archive(file)
11
+ File.join("test", "fixtures", file)
12
+ end
13
+
14
+ end
@@ -0,0 +1,41 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ class UnpackerTest < Test::Unit::TestCase
4
+
5
+ def test_extracting_files()
6
+ files.each do |target|
7
+ file = fixture_archive(target)
8
+ Unpacker.unpack(file) do |result|
9
+ assert result.entries.include?('archive-contents'), "#{target} was not extracted properly - entries contained #{result.entries.inspect}"
10
+ end
11
+ end
12
+ end
13
+
14
+ def test_extracting_gz
15
+ Unpacker.unpack(fixture_archive('lipsum.txt.gz')) do |result|
16
+ assert result.entries.include?('gz-contents'), "gz extracting did not work"
17
+ end
18
+ end
19
+
20
+ def test_valid_files
21
+ (files + ['lipsum.txt.gz']).each do |file|
22
+ assert Unpacker.valid? fixture_archive(file)
23
+ end
24
+ end
25
+
26
+ def test_invalid_files
27
+ invalid_files.each do |file|
28
+ assert ! Unpacker.valid?(fixture_archive(file))
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def files
35
+ %w[file.rar file.tar file.tar.gz file.tgz file.tar.bz file.tbz file.zip]
36
+ end
37
+
38
+ def invalid_files
39
+ %w[invalid-file.gz invalid-file.rar invalid-file.tar.gz invalid-file.zip]
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: underlog-unpacker
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Petyo Ivanov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-24 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bones
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.4.2
24
+ version:
25
+ description: Small library for unpacking various archives, extracted from Beanstalk (http://beanstalkapp.com) (and rewritten, in fact).
26
+ email: underlog@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - History.txt
33
+ - README.markdown
34
+ files:
35
+ - History.txt
36
+ - README.markdown
37
+ - Rakefile
38
+ - lib/unpacker.rb
39
+ - test/fixtures/archive-contents/sample-file
40
+ - test/fixtures/file.rar
41
+ - test/fixtures/file.tar
42
+ - test/fixtures/file.tar.bz
43
+ - test/fixtures/file.tar.gz
44
+ - test/fixtures/file.tbz
45
+ - test/fixtures/file.tgz
46
+ - test/fixtures/file.zip
47
+ - test/fixtures/invalid-file.gz
48
+ - test/fixtures/invalid-file.rar
49
+ - test/fixtures/invalid-file.tar.gz
50
+ - test/fixtures/invalid-file.zip
51
+ - test/fixtures/lipsum.txt.gz
52
+ - test/test_helper.rb
53
+ - test/unpacker_test.rb
54
+ has_rdoc: true
55
+ homepage: http://github.com/underlog/unpacker
56
+ post_install_message:
57
+ rdoc_options:
58
+ - --main
59
+ - README.markdown
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ version:
74
+ requirements: []
75
+
76
+ rubyforge_project: unpacker
77
+ rubygems_version: 1.2.0
78
+ signing_key:
79
+ specification_version: 2
80
+ summary: Small library for unpacking various archives, extracted from Beanstalk (http://beanstalkapp
81
+ test_files:
82
+ - test/test_helper.rb