zip_dir 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0fe1cd41c0b83dd595962ce0b0242b5aff1a4f4a
4
- data.tar.gz: 22106c6db7edcdd3af3172cba7f72a7bb2946bb0
3
+ metadata.gz: 22142ae2361dcab30f0d4ad978ff3260e696a4e2
4
+ data.tar.gz: 01e541d83f1ced7255b4fbfe17c9901fd3aba747
5
5
  SHA512:
6
- metadata.gz: 10207faa6757782af155611013bc82d18137d8d9b4d6e434d755e363a6a34da6a67a7d9cae0a85daf3eed48426403c847c0602ec5c59b3678f6fa62811b68866
7
- data.tar.gz: 32444d4ebd2e080675a721ee5022215777e9d04071d38274f834fd0dec10eb261aee573546346633c77935e303475785c914a1dbe8fcaa8a49f6dfb28bb34fe1
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
- zipper = ZipDir::Zipper.new
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.
@@ -20,7 +20,7 @@ module ZipDir
20
20
  end
21
21
 
22
22
  def unzip
23
- Zip::File.open(zip_path) do |zip_file|
23
+ ::Zip::File.open(zip_path) do |zip_file|
24
24
  zip_file.each do |entry|
25
25
  file_path = "#{@unzip_path}/#{entry.name}"
26
26
  entry.extract(file_path) unless File.exists?(file_path)
@@ -1,3 +1,3 @@
1
1
  module ZipDir
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -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
@@ -1,65 +1,60 @@
1
+ require "zip_dir/zip"
2
+
1
3
  module ZipDir
2
4
  class Zipper
3
- attr_reader :copy_path, :file
5
+ attr_reader :copy_path, :filename
4
6
 
5
- def initialize
6
- @copy_path = Dir.mktmpdir
7
- end
7
+ DEFAULT_FILENAME = "zipper.zip".freeze
8
8
 
9
- def add_path(source_path)
10
- FileUtils.cp_r source_path, copy_path
9
+ def initialize(filename=DEFAULT_FILENAME)
10
+ @filename = filename
11
11
  end
12
- alias_method :<<, :add_path
13
12
 
14
13
  def generated?
15
- !!file
14
+ !!@generated
16
15
  end
17
16
 
18
- def generate(filename="zipper")
19
- yield self
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
- def cleanup
24
- FileUtils.remove_entry_secure copy_path
25
- end
26
-
27
- class Zip
28
- attr_reader :source_path
29
- def initialize(source_path, filename)
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
- def file
34
- generate unless generated?
35
- @file
36
- end
29
+ @file = ZipDir::Zip.new(copy_path, filename).file
30
+ ensure
31
+ @generated = true
32
+ end
37
33
 
38
- def generate
39
- ::Zip::File.open(@file.path, ::Zip::File::CREATE) do |zip_io|
40
- zip_path(zip_io)
41
- end
42
- @generated = true
43
- end
34
+ def file
35
+ return unless generated?
36
+ @file
37
+ end
44
38
 
45
- def generated?
46
- !!@generated
47
- end
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
- def zip_path(zip_io, relative_path="")
50
- entries = Dir.clean_entries(relative_path.empty? ? source_path : File.join(source_path, relative_path))
51
- entries.each do |entry|
52
- relative_entry_path = relative_path.empty? ? entry : File.join(relative_path, entry)
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
- if File.directory?(source_entry_path)
56
- zip_io.mkdir relative_entry_path
57
- zip_path zip_io, relative_entry_path
58
- else
59
- zip_io.add relative_entry_path, source_entry_path
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
@@ -2,8 +2,6 @@ require "zip_dir/version"
2
2
 
3
3
  require 'zip'
4
4
 
5
- require 'zip_dir/core_ext/dir'
6
-
7
5
  require 'zip_dir/zipper'
8
6
  require 'zip_dir/unzipper'
9
7
 
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.0
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-10-27 00:00:00.000000000 Z
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
@@ -1,10 +0,0 @@
1
- class Dir
2
- class << self
3
- def clean_entries(dir)
4
- entries = entries(dir)
5
- entries.delete "."
6
- entries.delete ".."
7
- entries
8
- end
9
- end
10
- end