zip_dir 0.1.3 → 0.1.5

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: 1dc7143713cd26a4cbaf2ca5995e9bc7cf032cea
4
- data.tar.gz: 15faeef4d8857ed27ef8096053be6b780cdce1cb
3
+ metadata.gz: 79209ebb4653e9d8ffa94f40c0a0a8ecd7c94d25
4
+ data.tar.gz: 15c942c938fe5a8bd953242e0beedb278bb187b9
5
5
  SHA512:
6
- metadata.gz: e466840b0cbdcb230a3ba12cb37c43f6e1677e83b13358d95e940e36e39326ecbcbbbafcf2683cb78921fb4a7b38f9053569164b3baa722303f7bb7a6ef93b6a
7
- data.tar.gz: a8780a73ce554571ed041ead65a875922de9fbd0326b0bafcc224d902e0ab39b091c15f5820054ba9b0b3e449dc48f58314a87e90ff1232744045179d9d397bb
6
+ metadata.gz: aeccedb4f408d14756c527a519c676ba435ceb6e36644c5bd02df97b8f4d3f67531790e4bf1abf0da3e0a8bbcce2b8f280f1561140b6c7a8f0676cd5d46a6bf6
7
+ data.tar.gz: 54d959f438c11135d560c058cc8bec7d48b04f429ffe77077ec90b6091d01a92f420cbc48bea1d3a58d655a817c7800a105963a463cbd85ef3bc10f7368b30ac
data/README.md CHANGED
@@ -38,15 +38,44 @@ end
38
38
 
39
39
  # to zip __just the paths inside the directory___
40
40
  zip_file = zipper.generate("some/path/to/directory", root_directory: true)
41
-
41
+ # for multiple directories again
42
42
  zip_file = zipper.generate do |z|
43
43
  z.add_path "some/path/to/directory", root_directory: true
44
44
  end
45
45
 
46
+ # cleanup temporary directory
47
+ zipper.cleanup
48
+
46
49
  #
47
50
  # Unzip
48
51
  #
49
- unzip_path = ZipDir::Unzipper.new(zip_file.path).unzip_path # => "/var/folders/6c/s4snqy051jqdpbjw7f7tsn940000gn/T/d20151127-22683-a9vrnv"
52
+ ZipDir::Unzipper.new(zip_file.path).unzip_path # => "/var/folders/6c/s4snqy051jqdpbjw7f7tsn940000gn/T/d20151127-22683-a9vrnv"
53
+ ```
54
+
55
+ ## Dir Interface
56
+ `ZipDir::Zipper` subclasses `ZipDir::Dir`, which copies paths into a temporary directory to zip. There are several options for copying files and directories:
57
+
58
+ ```ruby
59
+ ZipDir::Zipper.superclass # => ZipDir::Dir
60
+
61
+ dir = ZipDir::Dir.new
62
+ dir.generate("some/path" options_described_below)
63
+ dir.generate do |d|
64
+ d.add_path "some/path", options_described_below
65
+ end
66
+
67
+ dir.cleanup
68
+ ```
69
+
70
+ ```ruby
71
+ # Directories
72
+ { root_directory: true } # copies each file and directory path within the given directory path
73
+ { flatten_directories: true } # copies all file paths with no directory structure copied (may override files)
74
+ { extension: :gif || ["gif", "txt"] || "*" } # filters file extensions of paths, used with options listed above (:extensions is an alias)
75
+ { glob: "custom_glob" } # copies the paths resulting from the custom glob
76
+
77
+ # Files and Directories
78
+ { rename: "new_name_with_smart_extension_handling" }
50
79
  ```
51
80
 
52
81
  ## dir_model
@@ -0,0 +1,83 @@
1
+ module ZipDir
2
+ class Dir
3
+ attr_reader :copy_path
4
+
5
+ def generated?
6
+ !!@generated
7
+ end
8
+
9
+ def generate(source_path=nil, options={})
10
+ cleanup if generated?
11
+
12
+ @copy_path = ::Dir.mktmpdir
13
+ proxy = Proxy.new(copy_path)
14
+ if source_path
15
+ raise "Should not give block and source_path: #{source_path}" if block_given?
16
+ proxy.add_path source_path, options
17
+ else
18
+ yield proxy
19
+ end
20
+
21
+ ::Dir.new(copy_path)
22
+ ensure
23
+ @generated = true
24
+ end
25
+
26
+ def cleanup
27
+ FileUtils.remove_entry_secure copy_path if copy_path
28
+ @copy_path = nil
29
+ @generated = false
30
+ end
31
+
32
+ class Proxy
33
+ def initialize(copy_path)
34
+ @copy_path = copy_path
35
+ end
36
+
37
+ def add_path(source_path, options={})
38
+ options = options.inject({}){|hash,(k,v)| hash[k.to_sym] = v; hash}
39
+
40
+ if File.directory?(source_path)
41
+ add_directory source_path, options
42
+ elsif File.file?(source_path)
43
+ add_simple_path source_path, options
44
+ else
45
+ raise "Attempting to add non-existent path: #{source_path}"
46
+ end
47
+ end
48
+ alias_method :<<, :add_path
49
+
50
+ protected
51
+ def add_directory(source_path, options={})
52
+ options[:extension] = options[:extensions] unless options[:extension]
53
+ glob = nil
54
+
55
+ if options[:glob]
56
+ glob = options[:glob]
57
+ elsif options[:flatten_directories]
58
+ glob = "#{source_path}/**/*"
59
+ options[:extension] = '*' unless options[:extension]
60
+ elsif options[:root_directory]
61
+ glob = "#{source_path}/*"
62
+ end
63
+
64
+ return add_simple_path source_path, options unless glob
65
+
66
+ glob += ".{#{Array[options[:extension]].join(",")}}" if options[:extension]
67
+ ::Dir.glob(glob).each { |path| add_simple_path(path) }
68
+ end
69
+
70
+ def add_simple_path(source_path, options={})
71
+ copy_path_parts = [@copy_path]
72
+
73
+ if options[:rename]
74
+ rename = options[:rename]
75
+ rename += File.extname(source_path) if File.extname(rename).empty?
76
+ copy_path_parts << rename
77
+ end
78
+
79
+ FileUtils.cp_r source_path, copy_path_parts.join("/")
80
+ end
81
+ end
82
+ end
83
+ end
@@ -2,7 +2,7 @@ module ZipDir
2
2
  class Unzipper
3
3
  attr_reader :zip_path
4
4
  def initialize(zip_path)
5
- @zip_path, @unzip_path = zip_path, Dir.mktmpdir
5
+ @zip_path, @unzip_path = zip_path, ::Dir.mktmpdir
6
6
  end
7
7
 
8
8
  def cleanup
@@ -24,9 +24,7 @@ module ZipDir
24
24
  zip_file.each do |entry|
25
25
  file_path = "#{@unzip_path}/#{entry.name}"
26
26
 
27
- # Fixes:
28
- # Errno::ENOENT:
29
- # No such file or directory @ rb_sysopen - /var/folders/6c/s4snqy051jqdpbjw7f7tsn940000gn/T/d20160209-56123-1o14n5n/Niveaux/Fondations.png
27
+ # Fixes: spec/fixtures/encoded_differently.zip (in a test)
30
28
  dir_path = File.dirname(file_path).to_s
31
29
  FileUtils.mkdir_p dir_path unless File.exists?(dir_path)
32
30
 
@@ -1,3 +1,3 @@
1
1
  module ZipDir
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.5"
3
3
  end
@@ -24,7 +24,7 @@ module ZipDir
24
24
 
25
25
  protected
26
26
  def zip_path(zip_io, relative_path="")
27
- entries = Dir.entries(relative_path.empty? ? source_path : File.join(source_path, relative_path))
27
+ entries = ::Dir.entries(relative_path.empty? ? source_path : File.join(source_path, relative_path))
28
28
  entries.delete(".")
29
29
  entries.delete("..")
30
30
  entries.each do |entry|
@@ -1,8 +1,9 @@
1
+ require "zip_dir/dir"
1
2
  require "zip_dir/zip"
2
3
 
3
4
  module ZipDir
4
- class Zipper
5
- attr_reader :copy_path, :filename
5
+ class Zipper < Dir
6
+ attr_reader :filename
6
7
 
7
8
  DEFAULT_FILENAME = "zipper.zip".freeze
8
9
 
@@ -10,51 +11,19 @@ module ZipDir
10
11
  @filename = filename
11
12
  end
12
13
 
13
- def generated?
14
- !!@generated
15
- end
16
-
17
14
  def generate(source_path=nil, root_directory: false)
18
- cleanup if generated?
19
-
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
27
- end
28
-
15
+ super
29
16
  @file = ZipDir::Zip.new(copy_path, filename).file
30
- ensure
31
- @generated = true
32
- end
33
-
34
- def file
35
- return unless generated?
36
- @file
37
17
  end
38
18
 
39
19
  def cleanup
40
- FileUtils.remove_entry_secure copy_path if copy_path
41
- @file = @copy_path = nil
42
- @generated = false
20
+ super
21
+ @file = nil
43
22
  end
44
23
 
45
- class Proxy
46
- def initialize(copy_path)
47
- @copy_path = copy_path
48
- end
49
-
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
55
- end
56
- end
57
- alias_method :<<, :add_path
24
+ def file
25
+ return unless generated?
26
+ @file
58
27
  end
59
28
  end
60
29
  end
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.3
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Chung
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-02-09 00:00:00.000000000 Z
11
+ date: 2016-03-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubyzip
@@ -41,6 +41,7 @@ files:
41
41
  - bin/console
42
42
  - bin/setup
43
43
  - lib/zip_dir.rb
44
+ - lib/zip_dir/dir.rb
44
45
  - lib/zip_dir/unzipper.rb
45
46
  - lib/zip_dir/version.rb
46
47
  - lib/zip_dir/zip.rb