zip_dir 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +20 -2
- data/lib/zip_dir/unzipper.rb +1 -1
- data/lib/zip_dir/version.rb +1 -1
- data/lib/zip_dir/zip.rb +42 -0
- data/lib/zip_dir/zipper.rb +39 -44
- data/lib/zip_dir.rb +0 -2
- metadata +3 -3
- data/lib/zip_dir/core_ext/dir.rb +0 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 22142ae2361dcab30f0d4ad978ff3260e696a4e2
|
4
|
+
data.tar.gz: 01e541d83f1ced7255b4fbfe17c9901fd3aba747
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d6851216876a02cb2bde2053e8ba72707a8d98abf9a18c2ae6732171bde48be53807a70ad372100a0ea37e130d8ae0d81f2dcc34ccb555c35adb5aee00f3a02b
|
7
|
+
data.tar.gz: fba12bc80581b2fae9166510c9f9b62aa1b9a7576aceea1d9dd74a86f7348053f9240e375c0a5adf87d3a31c105190b81f72e334ed44eba191ea35de776f8bfc
|
data/README.md
CHANGED
@@ -21,15 +21,33 @@ Or install it yourself as:
|
|
21
21
|
## Usage
|
22
22
|
|
23
23
|
```ruby
|
24
|
+
#
|
24
25
|
# Zip
|
25
|
-
|
26
|
+
#
|
27
|
+
zipper = ZipDir::Zipper.new(__optional_filename__)
|
28
|
+
|
29
|
+
zip_file = zipper.generate(__some_path_to_directory__)
|
30
|
+
zip_file # => #<Tempfile:/var/folders/6c/s4snqy051jqdpbjw7f7tsn940000gn/T/zipper-20151127-19694-1baaqoi.zip>
|
31
|
+
zip_file == zipper.file # => true
|
32
|
+
|
33
|
+
# to zip multiple directories
|
26
34
|
zip_file = zipper.generate do |z|
|
27
35
|
z.add_path __some_path_to_directory__
|
28
36
|
z.add_path __another_path_to_directory__ # does a shell "cp -r" operation
|
29
37
|
end
|
30
|
-
zip_file == zipper.file # => true
|
31
38
|
|
39
|
+
# to zip __just the paths inside the directory___
|
40
|
+
zip_file = zipper.generate(__some_path_to_directory__, root_directory: true)
|
32
41
|
|
42
|
+
zip_file = zipper.generate do |z|
|
43
|
+
z.add_path __some_path_to_directory__, root_directory: true
|
44
|
+
end
|
45
|
+
|
46
|
+
#
|
33
47
|
# Unzip
|
48
|
+
#
|
34
49
|
unzip_path = ZipDir::Unzipper.new(zip_file.path).unzip_path
|
35
50
|
```
|
51
|
+
|
52
|
+
## dir_model
|
53
|
+
Use [`dir_model`](https://github.com/FinalCAD/dir_model) to create complex directories to zip.
|
data/lib/zip_dir/unzipper.rb
CHANGED
data/lib/zip_dir/version.rb
CHANGED
data/lib/zip_dir/zip.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
module ZipDir
|
2
|
+
class Zip
|
3
|
+
attr_reader :source_path
|
4
|
+
def initialize(source_path, filename="zip.zip")
|
5
|
+
filename = filename.chomp(".zip")
|
6
|
+
@source_path, @file = source_path, Tempfile.new(["#{filename}-", ".zip"])
|
7
|
+
end
|
8
|
+
|
9
|
+
def file
|
10
|
+
generate unless generated?
|
11
|
+
@file
|
12
|
+
end
|
13
|
+
|
14
|
+
def generate
|
15
|
+
::Zip::File.open(@file.path, ::Zip::File::CREATE) do |zip_io|
|
16
|
+
zip_path(zip_io)
|
17
|
+
end
|
18
|
+
@generated = true
|
19
|
+
end
|
20
|
+
|
21
|
+
def generated?
|
22
|
+
!!@generated
|
23
|
+
end
|
24
|
+
|
25
|
+
protected
|
26
|
+
def zip_path(zip_io, relative_path="")
|
27
|
+
entries = Dir.entries(relative_path.empty? ? source_path : File.join(source_path, relative_path))
|
28
|
+
entries.shift(2)
|
29
|
+
entries.each do |entry|
|
30
|
+
relative_entry_path = relative_path.empty? ? entry : File.join(relative_path, entry)
|
31
|
+
source_entry_path = File.join(source_path, relative_entry_path)
|
32
|
+
|
33
|
+
if File.directory?(source_entry_path)
|
34
|
+
zip_io.mkdir relative_entry_path
|
35
|
+
zip_path zip_io, relative_entry_path
|
36
|
+
else
|
37
|
+
zip_io.add relative_entry_path, source_entry_path
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/zip_dir/zipper.rb
CHANGED
@@ -1,65 +1,60 @@
|
|
1
|
+
require "zip_dir/zip"
|
2
|
+
|
1
3
|
module ZipDir
|
2
4
|
class Zipper
|
3
|
-
attr_reader :copy_path, :
|
5
|
+
attr_reader :copy_path, :filename
|
4
6
|
|
5
|
-
|
6
|
-
@copy_path = Dir.mktmpdir
|
7
|
-
end
|
7
|
+
DEFAULT_FILENAME = "zipper.zip".freeze
|
8
8
|
|
9
|
-
def
|
10
|
-
|
9
|
+
def initialize(filename=DEFAULT_FILENAME)
|
10
|
+
@filename = filename
|
11
11
|
end
|
12
|
-
alias_method :<<, :add_path
|
13
12
|
|
14
13
|
def generated?
|
15
|
-
|
14
|
+
!!@generated
|
16
15
|
end
|
17
16
|
|
18
|
-
def generate(
|
19
|
-
|
20
|
-
@file = Zip.new(copy_path, filename).file
|
21
|
-
end
|
17
|
+
def generate(source_path=nil, root_directory: false)
|
18
|
+
cleanup if generated?
|
22
19
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
@source_path, @file = source_path, Tempfile.new([filename, ".zip"])
|
20
|
+
@copy_path = Dir.mktmpdir
|
21
|
+
proxy = Proxy.new(copy_path)
|
22
|
+
if source_path
|
23
|
+
raise "should not give block and source_path" if block_given?
|
24
|
+
proxy.add_path source_path, root_directory: root_directory
|
25
|
+
else
|
26
|
+
yield proxy
|
31
27
|
end
|
32
28
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
29
|
+
@file = ZipDir::Zip.new(copy_path, filename).file
|
30
|
+
ensure
|
31
|
+
@generated = true
|
32
|
+
end
|
37
33
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
@generated = true
|
43
|
-
end
|
34
|
+
def file
|
35
|
+
return unless generated?
|
36
|
+
@file
|
37
|
+
end
|
44
38
|
|
45
|
-
|
46
|
-
|
47
|
-
|
39
|
+
def cleanup
|
40
|
+
FileUtils.remove_entry_secure copy_path if copy_path
|
41
|
+
@file = @copy_path = nil
|
42
|
+
@generated = false
|
43
|
+
end
|
48
44
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
source_entry_path = File.join(source_path, relative_entry_path)
|
45
|
+
class Proxy
|
46
|
+
def initialize(copy_path)
|
47
|
+
@copy_path = copy_path
|
48
|
+
end
|
54
49
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
end
|
50
|
+
def add_path(source_path, root_directory: false)
|
51
|
+
if root_directory
|
52
|
+
Dir.glob("#{source_path}/*").each { |path| add_path(path) }
|
53
|
+
else
|
54
|
+
FileUtils.cp_r source_path, @copy_path
|
61
55
|
end
|
62
56
|
end
|
57
|
+
alias_method :<<, :add_path
|
63
58
|
end
|
64
59
|
end
|
65
60
|
end
|
data/lib/zip_dir.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zip_dir
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Steve Chung
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-11-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rubyzip
|
@@ -41,9 +41,9 @@ files:
|
|
41
41
|
- bin/console
|
42
42
|
- bin/setup
|
43
43
|
- lib/zip_dir.rb
|
44
|
-
- lib/zip_dir/core_ext/dir.rb
|
45
44
|
- lib/zip_dir/unzipper.rb
|
46
45
|
- lib/zip_dir/version.rb
|
46
|
+
- lib/zip_dir/zip.rb
|
47
47
|
- lib/zip_dir/zipper.rb
|
48
48
|
- zip_dir.gemspec
|
49
49
|
homepage: https://github.com/FinalCAD/zip_dir
|