unzipMe 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +14 -0
- data/Gemfile.lock +31 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +40 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/lib/unzipMe.rb +3 -0
- data/lib/unzip_me/unzipme_exception.rb +3 -0
- data/lib/unzip_me/unzipme_unzipper.rb +54 -0
- data/lib/unzip_me/unzipme_validator.rb +77 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/unzipme_unzipper_spec.rb +21 -0
- data/spec/unzipme_validator_spec.rb +11 -0
- metadata +112 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "rspec", ">= 2.8.0"
|
10
|
+
gem "rdoc", ">= 3.12"
|
11
|
+
gem "bundler", ">= 1.0.0"
|
12
|
+
gem "jeweler", "~> 1.8.4"
|
13
|
+
#gem "rcov", ">= 0"
|
14
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.3)
|
5
|
+
git (1.2.5)
|
6
|
+
jeweler (1.8.4)
|
7
|
+
bundler (~> 1.0)
|
8
|
+
git (>= 1.2.5)
|
9
|
+
rake
|
10
|
+
rdoc
|
11
|
+
json (1.7.5)
|
12
|
+
rake (10.0.2)
|
13
|
+
rdoc (3.12)
|
14
|
+
json (~> 1.4)
|
15
|
+
rspec (2.12.0)
|
16
|
+
rspec-core (~> 2.12.0)
|
17
|
+
rspec-expectations (~> 2.12.0)
|
18
|
+
rspec-mocks (~> 2.12.0)
|
19
|
+
rspec-core (2.12.0)
|
20
|
+
rspec-expectations (2.12.0)
|
21
|
+
diff-lcs (~> 1.1.3)
|
22
|
+
rspec-mocks (2.12.0)
|
23
|
+
|
24
|
+
PLATFORMS
|
25
|
+
ruby
|
26
|
+
|
27
|
+
DEPENDENCIES
|
28
|
+
bundler (>= 1.0.0)
|
29
|
+
jeweler (~> 1.8.4)
|
30
|
+
rdoc (>= 3.12)
|
31
|
+
rspec (>= 2.8.0)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Kendra Lawlor
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
= unzipMe
|
2
|
+
|
3
|
+
If your application is attempting to handle extracting Zip files with the system unzip command but has failures AND
|
4
|
+
they can be unzipped by alternative means, this provides the 7zip failover as a second-in-line attempt at extraction.
|
5
|
+
|
6
|
+
== UnzipmeUnzipper
|
7
|
+
|
8
|
+
set instance to UnzipmeUnzipper.new(path_and_filename, {:extract_to => path}).
|
9
|
+
The second parameter is an optional hash with the :extract_to key. This is used if you want to extract the files to a different directory.
|
10
|
+
|
11
|
+
.new -> will validate the file being passed in, and if not readable through either unzip or 7za wil raise an UnzipmeException.
|
12
|
+
|
13
|
+
call instance.unzip -> this will extract the files.
|
14
|
+
call instance.file_list -> this will return a clean array of filenames within the zip archive with the header and trailer information removed.
|
15
|
+
|
16
|
+
==UnzipmeValidator
|
17
|
+
|
18
|
+
(The files are validated when initializing UnzipmeUnzipper, but this can be called just for testing the archive)
|
19
|
+
|
20
|
+
set instance to UnzipmeValidator.new(path_and_filename). This will test the archive but not return an exception if not valid.
|
21
|
+
instance.valid_zip? -> returns true if readable by either unzip or 7zip.
|
22
|
+
instance.command -> will return which command (unzip or 7ip that will be able to extract the file).
|
23
|
+
instance.error_message -> will return a reable message that lists the system error message (determined by the exit code) for both unzip and 7za.
|
24
|
+
|
25
|
+
|
26
|
+
== Contributing to unzipMe
|
27
|
+
|
28
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
29
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
30
|
+
* Fork the project.
|
31
|
+
* Start a feature/bugfix branch.
|
32
|
+
* Commit and push until you are happy with your contribution.
|
33
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
34
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
35
|
+
|
36
|
+
== Copyright
|
37
|
+
|
38
|
+
Copyright (c) 2012 Kendra Lawlor. See LICENSE.txt for
|
39
|
+
further details.
|
40
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "unzipMe"
|
18
|
+
gem.homepage = "http://github.com/kendra/unzipMe"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{using system unzip command and then 7zip as failover}
|
21
|
+
gem.description = %Q{using system unzip command and then 7zip as failover}
|
22
|
+
gem.email = "lawlorkendra@gmail.com"
|
23
|
+
gem.authors = ["Kendra Lawlor"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
+
end
|
33
|
+
|
34
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
35
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
36
|
+
spec.rcov = true
|
37
|
+
end
|
38
|
+
|
39
|
+
task :default => :spec
|
40
|
+
|
41
|
+
require 'rdoc/task'
|
42
|
+
Rake::RDocTask.new do |rdoc|
|
43
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
44
|
+
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = "unzipMe #{version}"
|
47
|
+
rdoc.rdoc_files.include('README*')
|
48
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
data/lib/unzipMe.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
|
2
|
+
class UnzipmeUnzipper
|
3
|
+
|
4
|
+
def initialize(file, args={})
|
5
|
+
validate_file(file)
|
6
|
+
@args = args
|
7
|
+
@file = file
|
8
|
+
end
|
9
|
+
|
10
|
+
def unzip
|
11
|
+
run_system_unzip if @command == UnzipmeValidator::COMMAND_UNZIP
|
12
|
+
run_system_7zip if @command == UnzipmeValidator::COMMAND_7ZIP
|
13
|
+
end
|
14
|
+
|
15
|
+
def file_list
|
16
|
+
files = []
|
17
|
+
|
18
|
+
listing_lines = IO.popen("unzip -l '#{@file}' 2>/dev/null") { |f| f.readlines } if @command == UnzipmeValidator::COMMAND_UNZIP
|
19
|
+
listing_lines = IO.popen("7za l '#{@file}' 2>/dev/null") {|f| f.readlines} if @command == UnzipmeValidator::COMMAND_7ZIP
|
20
|
+
|
21
|
+
listing_lines[0..-3].each_with_index do |line, idx| # The slice means ignore the last 3 lines of the list (just trailer information)
|
22
|
+
next if idx < 3 # Skip the first three lines; just header information
|
23
|
+
files << line.rstrip[27 + 1..-1] # The file name is the last column in the listing.
|
24
|
+
end
|
25
|
+
files
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def validate_file(file)
|
31
|
+
zip_validator = UnzipmeValidator.new(file)
|
32
|
+
raise(UnzipmeException, "#{zip_validator.error_message}") unless zip_validator.valid_zip?
|
33
|
+
@command = zip_validator.command
|
34
|
+
end
|
35
|
+
|
36
|
+
def run_system_unzip
|
37
|
+
if @args.has_key?(:extract_to)
|
38
|
+
system("unzip -d \"#{@args[:extract_to]}\" \"#{@file}\" > /dev/null")
|
39
|
+
else
|
40
|
+
system("unzip \"#{@file}\" > /dev/null")
|
41
|
+
end
|
42
|
+
$?.exitstatus
|
43
|
+
end
|
44
|
+
|
45
|
+
def run_system_7zip
|
46
|
+
if @args.has_key?(:extract_to)
|
47
|
+
system("7za x \"#{@file}\" -o\"#{@args[:extract_to]}\" > /dev/null")
|
48
|
+
else
|
49
|
+
system("7za x \"#{@file}\" > /dev/null")
|
50
|
+
end
|
51
|
+
$?.exitstatus
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
class UnzipmeValidator
|
2
|
+
|
3
|
+
SUCCESS = 0
|
4
|
+
COMMAND_UNZIP = 0
|
5
|
+
COMMAND_7ZIP = 1
|
6
|
+
|
7
|
+
def initialize(file)
|
8
|
+
raise(ArgumentError, "You must provide a file.") unless file
|
9
|
+
@file = file
|
10
|
+
test_quietly
|
11
|
+
end
|
12
|
+
|
13
|
+
def valid_zip?
|
14
|
+
@unzip_status == SUCCESS || @sevenzip_status == SUCCESS
|
15
|
+
end
|
16
|
+
|
17
|
+
def command
|
18
|
+
return COMMAND_UNZIP if @unzip_status == SUCCESS
|
19
|
+
return COMMAND_7ZIP if @sevenzip_status == SUCCESS
|
20
|
+
end
|
21
|
+
|
22
|
+
def error_message
|
23
|
+
"Unzip error: #{unzip_error(@unzip_status)}, 7zip error: #{seven_zip_error(@sevenzip_status)}"
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def test_quietly()
|
29
|
+
@unzip_status = test_with_system_unzip
|
30
|
+
@sevenzip_status = test_with_system_7zip
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_with_system_unzip
|
34
|
+
system "unzip -P '' -tq '#{@file}' > /dev/null 2>&1"
|
35
|
+
$?.exitstatus
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_with_system_7zip
|
39
|
+
system("7za t \"#{@file}\" > /dev/null")
|
40
|
+
$?.exitstatus
|
41
|
+
end
|
42
|
+
|
43
|
+
def unzip_error(exit_code)
|
44
|
+
case exit_code
|
45
|
+
when 1 then "one or more warning errors were encountered."
|
46
|
+
when 2 then "a generic error in the zipfile format was detected."
|
47
|
+
when 3 then "a severe error in the zipfile format was detected."
|
48
|
+
when 4 then "unzip was unable to allocate memory for one or more buffers during program initialization."
|
49
|
+
when 5 then "unzip was unable to allocate memory or unable to obtain a tty to read the decryption password(s)."
|
50
|
+
when 6 then "unzip was unable to allocate memory during decompression to disk."
|
51
|
+
when 7 then "unzip was unable to allocate memory during in-memory decompression."
|
52
|
+
when 9 then "the specified zipfiles were not found."
|
53
|
+
when 10 then "invalid options were specified on the command line."
|
54
|
+
when 11 then "no matching files were found."
|
55
|
+
when 50 then "the disk is (or was) full during extraction"
|
56
|
+
when 51 then "the end of the ZIP archive was encountered prematurely."
|
57
|
+
when 80 then "the user aborted unzip prematurely with control-C (or similar)"
|
58
|
+
when 81 then "testing or extraction of one or more files failed due to unsupported compression methods or unsupported decryption."
|
59
|
+
when 82 then "no files were found due to bad decryption password(s)."
|
60
|
+
else ""
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def seven_zip_error(exit_code)
|
65
|
+
case exit_code
|
66
|
+
when 1 then "Warning (Non fatal error(s))."
|
67
|
+
when 2 then "Fatal error."
|
68
|
+
when 7 then "Command line error."
|
69
|
+
when 8 then "Not enough memory for operation."
|
70
|
+
when 255 then "User stopped the process."
|
71
|
+
else ""
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
end
|
77
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'unzipMe'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
|
12
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe UnzipmeUnzipper do
|
4
|
+
|
5
|
+
it "raises an exception unless a file is provided" do
|
6
|
+
expect {UnzipmeUnzipper.new(nil)}.to raise_error(ArgumentError, "You must provide a file.")
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def mock_file
|
13
|
+
mock(File)
|
14
|
+
end
|
15
|
+
|
16
|
+
def mock_directory
|
17
|
+
mock(Dir)
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe UnzipmeValidator do
|
4
|
+
|
5
|
+
it "should raise an ArgumentError if initialized with nil" do
|
6
|
+
expect {UnzipmeValidator.new(nil)}.to raise_error(ArgumentError, "You must provide a file.")
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: unzipMe
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 1.0.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kendra Lawlor
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2012-12-07 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.8.0
|
24
|
+
type: :development
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rdoc
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: "3.12"
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id002
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: bundler
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 1.0.0
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id003
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: jeweler
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ~>
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 1.8.4
|
57
|
+
type: :development
|
58
|
+
version_requirements: *id004
|
59
|
+
description: using system unzip command and then 7zip as failover
|
60
|
+
email: lawlorkendra@gmail.com
|
61
|
+
executables: []
|
62
|
+
|
63
|
+
extensions: []
|
64
|
+
|
65
|
+
extra_rdoc_files:
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.rdoc
|
68
|
+
files:
|
69
|
+
- .document
|
70
|
+
- .rspec
|
71
|
+
- Gemfile
|
72
|
+
- Gemfile.lock
|
73
|
+
- LICENSE.txt
|
74
|
+
- README.rdoc
|
75
|
+
- Rakefile
|
76
|
+
- VERSION
|
77
|
+
- lib/unzipMe.rb
|
78
|
+
- lib/unzip_me/unzipme_exception.rb
|
79
|
+
- lib/unzip_me/unzipme_unzipper.rb
|
80
|
+
- lib/unzip_me/unzipme_validator.rb
|
81
|
+
- spec/spec_helper.rb
|
82
|
+
- spec/unzipme_unzipper_spec.rb
|
83
|
+
- spec/unzipme_validator_spec.rb
|
84
|
+
homepage: http://github.com/kendra/unzipMe
|
85
|
+
licenses:
|
86
|
+
- MIT
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options: []
|
89
|
+
|
90
|
+
require_paths:
|
91
|
+
- lib
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: "0"
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: "0"
|
104
|
+
requirements: []
|
105
|
+
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 1.8.23
|
108
|
+
signing_key:
|
109
|
+
specification_version: 3
|
110
|
+
summary: using system unzip command and then 7zip as failover
|
111
|
+
test_files: []
|
112
|
+
|