nispack 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
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in nispack.gemspec
4
+ gemspec
@@ -0,0 +1,10 @@
1
+ #Nispack
2
+
3
+ ###Install
4
+ gem install nispack
5
+
6
+ ###Usage
7
+ run nispack --help
8
+
9
+
10
+
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,70 @@
1
+ #!/usr/bin/env ruby
2
+ require "nispack"
3
+ require "getopt/long"
4
+
5
+ def help
6
+ puts "nispack tool by tonypolik"
7
+ puts "version: #{Nispack::VERSION}"
8
+ puts " Usage:"
9
+ puts " nispack -f file.dat -e file1 -e file2"
10
+ puts " extract file1 and file2 from file.dat"
11
+ puts " nispack -f file.dat -u"
12
+ puts " extract ALL files from file.dat in a default directory"
13
+ puts " nispack -f file.dat -u -d dir"
14
+ puts " extract all files from file.dat in a directory named dir"
15
+ puts " nispack -f file.dat -c"
16
+ puts " create a new nispack file named file.dat using all files in the current directory"
17
+ puts " nispack -f file.dat -c -d dir"
18
+ puts " create a new nispack file named file.dat using all files in the 'dir' directory"
19
+ exit
20
+ end
21
+
22
+ help if ARGV == []
23
+
24
+ opts = Getopt::Long.getopts(
25
+ ["--help", "-h", Getopt::BOOLEAN],
26
+ ["--file", "-f", Getopt::REQUIRED],
27
+ ["--unpack", "-u", Getopt::BOOLEAN],
28
+ ["--extract", "-e", Getopt::REQUIRED],
29
+ ["--create", "-c", Getopt::BOOLEAN],
30
+ ["--dir", "-d", Getopt::REQUIRED]
31
+ )
32
+
33
+ help if opts["help"]
34
+
35
+ unless opts.has_key?("file")
36
+ raise "Must use the -f or --file flag to select a nispack file to read or create."
37
+ end
38
+
39
+ a, b, c = "extract", "create", "unpack"
40
+ 3.times do
41
+ if opts.has_key?(a) and (opts.has_key?(b) or opts.has_key?(c))
42
+ raise "Can't use --#{a} with --#{b} or -- {c}"
43
+ end
44
+ x=a; a=b; b=c; c=x
45
+ end
46
+
47
+
48
+ if opts["extract"] and opts["dir"]
49
+ raise "Can't use --dir with --extract"
50
+ end
51
+
52
+ case opts["extract"]
53
+ when String; Nispack.open(opts["file"]) {|n| n.extract(opts["extract"])}
54
+ when Array; opts["extract"].each {|x| Nispack.open(opts["file"]) {|n| n.extract(x)}}
55
+ end
56
+
57
+ if opts["unpack"]
58
+ case opts["dir"]
59
+ when nil; Nispack.open(opts["file"]) {|n| n.extract_all}
60
+ else; Nispack.open(opts["file"]) {|n| n.extract_all(opts["dir"])}
61
+ end
62
+ end
63
+
64
+ if opts["create"]
65
+ case opts["dir"]
66
+ when nil; Nispack.create_from_directory(:name => opts["file"])
67
+ else; Nispack.create_from_directory(:name => opts["file"], :dir => opts["dir"])
68
+ end
69
+ end
70
+
@@ -0,0 +1,98 @@
1
+ require "fileutils"
2
+
3
+
4
+ class Nispack
5
+
6
+ VERSION = "1.0.0"
7
+ SIGNATURE = "NISPACK\0\0\0\0\0".force_encoding("binary")
8
+
9
+ attr_reader :filename
10
+ attr_reader :file
11
+ attr_reader :entries
12
+
13
+ def initialize(name, file, entries)
14
+ @name = name
15
+ @file = file
16
+ @entries = entries
17
+ end
18
+
19
+ def self.open(filename)
20
+ name = File.basename(filename)
21
+ file = File.new(filename, "rb")
22
+ if file.read(SIGNATURE.bytesize) != SIGNATURE
23
+ raise "#{filename} is not a Nispack: wrong signature."
24
+ end
25
+ n_entries = file.read(4).unpack('l')[0]
26
+ entries = Array.new(n_entries)
27
+ entries.map! do |entry|
28
+ entry = Hash.new
29
+ entry[:name] = file.read(32).strip
30
+ entry[:start] = file.read(4).unpack('l')[0]
31
+ entry[:size] = file.read(4).unpack('l')[0]
32
+ file.pos += 4
33
+ entry
34
+ end
35
+ file.pos = 0
36
+ nispack = self.new(name, file, entries)
37
+ if block_given?
38
+ yield nispack
39
+ nispack.close
40
+ else
41
+ return nispack
42
+ end
43
+ end
44
+
45
+ def close
46
+ @file.close
47
+ end
48
+
49
+ def extract_all(dir = File.basename(@name).chomp(File.extname(@name)))
50
+ old_dir = Dir.pwd
51
+ FileUtils.mkdir_p(dir)
52
+ FileUtils.cd(dir)
53
+ @entries.each {|entry| extract_entry(entry)}
54
+ FileUtils.cd(old_dir)
55
+ end
56
+
57
+ def extract(name)
58
+ entry = @entries.select{|x| x[:name] == name}.first
59
+ if entry.nil?
60
+ raise "Can't extract #{name}: not in the Nispack archive"
61
+ end
62
+ extract_entry(entry)
63
+ end
64
+
65
+ def extract_entry(entry)
66
+ File.open(entry[:name], "wb") do |out|
67
+ @file.pos = entry[:start]
68
+ out.write(@file.read(entry[:size]))
69
+ @file.pos = 0
70
+ end
71
+ end
72
+
73
+ private :extract_entry
74
+
75
+ def self.create_from_directory(options)
76
+ dir = options[:dir] || Dir.pwd
77
+ name = options[:name] || File.basename(Dir.pwd) + ".DAT"
78
+ files = Dir.entries(dir).select{|f| File.file?(File.join(dir, f))}
79
+ out = File.new(name, "wb+")
80
+ out.write(SIGNATURE)
81
+ out.write([files.size].pack("l"))
82
+ start = 16 + 44 * files.size
83
+ files.each do |f|
84
+ out.write([f].pack("a32"))
85
+ out.write([start].pack("l"))
86
+ size = File.size(File.join(dir, f))
87
+ out.write([size].pack("l"))
88
+ out.write([0].pack("l"))
89
+ start += size
90
+ end
91
+ files.each do |f|
92
+ out.write(IO.binread(File.join(dir, f)))
93
+ end
94
+ out.close
95
+ end
96
+
97
+
98
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "nispack"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "nispack"
7
+ s.version = Nispack::VERSION
8
+ s.authors = ["tonypolik"]
9
+ s.email = ["tonypolik@gmail.com"]
10
+ s.homepage = "https://github.com/tonypolik"
11
+ s.summary = %q{Easy handling of NISPACK files.}
12
+ s.description = %q{Easy handling of NISPACK files. You can extract the files or create a new archive from a directory.}
13
+
14
+ s.rubyforge_project = "nispack"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ s.add_runtime_dependency "getopt"
24
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nispack
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.0.0
6
+ platform: ruby
7
+ authors:
8
+ - tonypolik
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-09-26 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: getopt
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ description: Easy handling of NISPACK files. You can extract the files or create a new archive from a directory.
27
+ email:
28
+ - tonypolik@gmail.com
29
+ executables:
30
+ - nispack
31
+ extensions: []
32
+
33
+ extra_rdoc_files: []
34
+
35
+ files:
36
+ - .gitignore
37
+ - Gemfile
38
+ - README.markdown
39
+ - Rakefile
40
+ - bin/nispack
41
+ - lib/nispack.rb
42
+ - nispack.gemspec
43
+ homepage: https://github.com/tonypolik
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options: []
48
+
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ requirements: []
64
+
65
+ rubyforge_project: nispack
66
+ rubygems_version: 1.8.10
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Easy handling of NISPACK files.
70
+ test_files: []
71
+