zipbundler 0.2.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: '0368dd29edd6b16d0591927891ba34bc871cf884'
4
+ data.tar.gz: 6983437e6e3cc87fd1a713fb7460835d5d39365c
5
+ SHA512:
6
+ metadata.gz: d8fec8fafec3cb56c2757257a2e6c3733f590b102260707bb37ca6ebf108d91d4fc4ff8cfd929d8fa2fe95846b6fa7f2499aa44e6f5f6804cd3fef4f2bb62684
7
+ data.tar.gz: 62dba7fbb13fc87dd1c7118714025f126fac9dfc436b946949898f412e02f4c7cb2c6857812e626dcbf4a0420171a96630d3d856de5f68aefdb195b312ed301b
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at leomichine@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in zipper.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Le0Michine
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # Zipper
2
+
3
+ Utility intended to simplify zip packages creating.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'zipper'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install zipper
20
+
21
+ ## Usage
22
+
23
+ First you need to create an instance of `ZipFileGenerator`:
24
+
25
+ ZipFileGenerator.new(json_config)
26
+
27
+ `json_config` is a hash variable which can be simply created from a json file. It should has the following structure:
28
+
29
+ {
30
+ "name": "out.zip",
31
+ "entries": [
32
+ "path_to_file",
33
+ "path_to_directory", // real path in file system to file or directory
34
+ {
35
+ "type": "file", // type of entry, if absent will be treated as 'file'
36
+ "name": "test.json", // real path in file system to file or directory
37
+ "path": "new_path_in_zip" // optional
38
+ },
39
+ {
40
+ "type": "zip", // nested zip archive
41
+ "name": "test.zip", // name of nested zip archive, can include directories. 'path' property is being ignored for this kind of entries
42
+ "entries": [ // array of entries for nested zip file, same format as above
43
+ "file",
44
+ "dir"
45
+ ],
46
+ "ignoreEntries": [ ... ] // local array of entries to ignore
47
+ }
48
+ ],
49
+ "ignoreEntries": [
50
+ ".DS_Store"
51
+ ]
52
+ }
53
+
54
+ ## Development
55
+
56
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
57
+
58
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
59
+
60
+ ## Contributing
61
+
62
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/zipper. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
63
+
64
+
65
+ ## License
66
+
67
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
68
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "zipper"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,96 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-04/schema#",
3
+ "description": "Schema of configuration",
4
+ "definitions": {
5
+ "config": {
6
+ "type": "object",
7
+ "description": "root configuration object",
8
+ "properties": {
9
+ "name": {
10
+ "type": "string",
11
+ "description": "name of output file, can include relative path"
12
+ },
13
+ "entries": {
14
+ "type": "array",
15
+ "description": "array of items to be compressed",
16
+ "items": {
17
+ "$ref": ["#/definitions/file_entry", "#/definitions/zip_entry", "#/definitions/string_entry"]
18
+ }
19
+ }
20
+ }
21
+ },
22
+ "string_entry": {
23
+ "type": "string",
24
+ "description": "path to file or directory to include into zip package"
25
+ },
26
+ "file_entry": {
27
+ "type": "object",
28
+ "description": "item which will be included into zip package",
29
+ "properties": {
30
+ "type": {
31
+ "type": "string",
32
+ "description": "type of entry, should be set to 'file' or omitted",
33
+ "default": "file",
34
+ "enum": [
35
+ "zip",
36
+ "file"
37
+ ]
38
+ },
39
+ "name": {
40
+ "type": "string",
41
+ "description": "path to file or directory to include into zip package"
42
+ },
43
+ "path": {
44
+ "type": "string",
45
+ "description": "path to file or directory which will be used inside zip package"
46
+ }
47
+ },
48
+ "required": [
49
+ "name",
50
+ "path"
51
+ ]
52
+ },
53
+ "zip_entry": {
54
+ "type": "object",
55
+ "description": "nested zip package",
56
+ "properties": {
57
+ "type": {
58
+ "type": "string",
59
+ "description": "type of entry, should be set to 'zip'"
60
+ },
61
+ "name": {
62
+ "type": "string",
63
+ "description": "path to file or directory to include into zip package"
64
+ },
65
+ "entries": {
66
+ "type": "array",
67
+ "description": "array of items to be compressed",
68
+ "items": {
69
+ "$ref": ["#/definitions/file_entry", "#/definitions/zip_entry", "#/definitions/string_entry"]
70
+ }
71
+ },
72
+ "ignoreEntries": {
73
+ "type": "array",
74
+ "description": "array of items to be ignored",
75
+ "items": {
76
+ "type": "string",
77
+ "description": "path or part of the path to a file or folder which should be ignored"
78
+ }
79
+ }
80
+ },
81
+ "ignoreEntries": {
82
+ "type": "array",
83
+ "description": "array of items to be ignored",
84
+ "items": {
85
+ "type": "string",
86
+ "description": "path or part of the path to a file or folder which should be ignored"
87
+ }
88
+ },
89
+ "required": [
90
+ "name",
91
+ "type",
92
+ "entries"
93
+ ]
94
+ }
95
+ }
96
+ }
@@ -0,0 +1,29 @@
1
+ module Zipper
2
+ class FileEntry
3
+ def initialize(file_name, entry_type = FileEntryType::FILE, flatten_structure = false)
4
+ @type = entry_type
5
+ @name = file_name
6
+ @flatten = flatten_structure
7
+ end
8
+
9
+ def add_buffer(buffer)
10
+ @buffer = buffer
11
+ end
12
+
13
+ def type
14
+ @type
15
+ end
16
+
17
+ def name
18
+ @name
19
+ end
20
+
21
+ def flatten
22
+ @flatten
23
+ end
24
+
25
+ def buffer
26
+ @buffer
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,6 @@
1
+ module Zipper
2
+ module FileEntryType
3
+ ZIP = "ZIP"
4
+ FILE = "FILE"
5
+ end
6
+ end
@@ -0,0 +1,10 @@
1
+ module Zipper
2
+ module JsonConfigKey
3
+ Type = "type"
4
+ Name = "name"
5
+ Path = "path"
6
+ Flatten = "flatten"
7
+ Entries = "entries"
8
+ IgnoreEntries = "ignoreEntries"
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module Zipper
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,16 @@
1
+ module Zipper
2
+ class ZipEntry
3
+ def initialize(file_name, buffer)
4
+ @name = file_name
5
+ @buffer = buffer
6
+ end
7
+
8
+ def name
9
+ @name
10
+ end
11
+
12
+ def buffer
13
+ @buffer
14
+ end
15
+ end
16
+ end
data/lib/zipper.rb ADDED
@@ -0,0 +1,102 @@
1
+ require "zipper/version"
2
+ require "zipper/fileEntryType"
3
+ require "zipper/fileEntry"
4
+ require "zipper/zipEntry"
5
+ require "zipper/JsonConfigKey"
6
+ require "zip"
7
+
8
+ module Zipper
9
+ class ZipFileGenerator
10
+ # Initialize with the json config.
11
+ def initialize(json_config)
12
+ @entries = json_config[JsonConfigKey::Entries]
13
+ @ignore_entries = json_config[JsonConfigKey::IgnoreEntries]
14
+ @output_file = json_config[JsonConfigKey::Name]
15
+ end
16
+
17
+ # Zip the input entries.
18
+ def write
19
+ buffer = create_zip(@entries, @ignore_entries)
20
+
21
+ puts "\nwrite file #{@output_file}"
22
+ File.open(@output_file, "wb") {|f| f.write buffer.string }
23
+ end
24
+
25
+ private
26
+
27
+ # True if +fileEntry+ isn't included into +ignore_entries+ array
28
+ # Params:
29
+ # +file_entry_name+:: name of the entry
30
+ # +file_entry_type+:: type of the entry
31
+ # +ignore_entries+:: array of entries for which should be excluded (false returned)
32
+ def filter_entries(file_entry_name, file_entry_type, ignore_entries)
33
+ return true if ignore_entries == nil
34
+ ignore_entries.each do |entry|
35
+ return false if (file_entry_type.casecmp(FileEntryType::FILE) == 0) && (file_entry_name.include? entry)
36
+ end
37
+ return true
38
+ end
39
+
40
+ # Creates +FileEntry+ for file or array of entries for directory and subdirectories
41
+ # Params:
42
+ # +directory_or_file+:: path to directory or file
43
+ # +entry_path+:: path with which the entry should be put into zip
44
+ def get_entries(directory_or_file, entry_path, ignore_entries)
45
+ if File.directory? directory_or_file
46
+ get_dir_entries_recursively(directory_or_file, entry_path, ignore_entries)
47
+ else
48
+ FileEntry.new(directory_or_file, FileEntryType::FILE, false, entry_path)
49
+ end
50
+ end
51
+
52
+ # Collects all files from directory recursively
53
+ # Params:
54
+ # +dir+:: start directory
55
+ # +entry_path+:: path with which file should be placed into zip
56
+ # +replace_path+:: part of path which is being replaced by +entry_path+
57
+ def get_dir_entries_recursively(dir, entry_path, ignore_entries, replace_path = nil)
58
+ replace_path = dir.clone if replace_path.nil?
59
+ (Dir.entries(dir) - %w(. ..)).map { |v| File.join(dir, v) }.select { |path| filter_entries path, FileEntryType::FILE, ignore_entries }.map { |path|
60
+ if File.directory? path
61
+ get_dir_entries_recursively(path, entry_path, ignore_entries, replace_path)
62
+ else
63
+ entry_path_in_zip = (entry_path.nil? ? path : path.sub(replace_path, entry_path)).gsub(/^[\/\\]+/, "")
64
+ FileEntry.new(path, FileEntryType::FILE, false, entry_path_in_zip)
65
+ end
66
+ }
67
+ end
68
+
69
+ # Creates zip file in memory from passed +FileEntry+ array, returns StringIO as result
70
+ # Params:
71
+ # +entries+:: array of +FileEntry+ and +ZipEntry+ objects
72
+ def compress(entries)
73
+ puts "\nadding the following entries into zip package"
74
+ puts "#{ entries.map{ |x| x.name + ", " + x.path.to_s + ", " + x.type.to_s }.join("\n")}"
75
+ buffer = Zip::File.add_buffer do |zio|
76
+ entries.each do |file|
77
+ if file.is_a FileEntry
78
+ zio.add(file.path == nil ? file.name : file.path, file.name)
79
+ else
80
+ zio.get_output_stream(file.name) { |os| os.write file.buffer.string }
81
+ end
82
+ end
83
+ end
84
+ end
85
+
86
+ # Creates from json array of entries
87
+ # Params:
88
+ # +entries+:: input entries to compress
89
+ # +ignore_entries+:: entries which should be ignored
90
+ def create_zip(entries, ignore_entries)
91
+ compress(entries.map { |x|
92
+ if x.is_a? String
93
+ get_entries(x, nil, ignore_entries)
94
+ elsif x[JsonConfigKey::Type].nil? || x[JsonConfigKey::Type].casecmp(FileEntryType::FILE) == 0
95
+ get_entries(x[JsonConfigKey::Name], x[JsonConfigKey::Path], ignore_entries)
96
+ elsif x[JsonConfigKey::Type].casecmp(FileEntryType::ZIP) == 0
97
+ ZipEntry.new(x[JsonConfigKey::Name], create_zip(x[JsonConfigKey::Entries], x[JsonConfigKey::IgnoreEntries]))
98
+ end
99
+ }.flatten.select{ |f| filter_entries(f.name, f.type, ignore_entries) }.uniq{ |f| f.name })
100
+ end
101
+ end
102
+ end
data/zipper.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'zipper/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "zipbundler"
8
+ spec.version = Zipper::VERSION
9
+ spec.authors = ["Le0Michine"]
10
+ spec.email = ["leomichine@gmail.com"]
11
+
12
+ spec.summary = %q{Creates zip packages with complex structure.}
13
+ spec.description = %q{Creates zip packages with complex structure.}
14
+ spec.homepage = "https://github.com/Le0Michine/zipper"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "rubyzip"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.14"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zipbundler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Le0Michine
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-04-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rubyzip
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.14'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Creates zip packages with complex structure.
56
+ email:
57
+ - leomichine@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - CODE_OF_CONDUCT.md
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - config_schema.json
71
+ - lib/zipper.rb
72
+ - lib/zipper/fileEntry.rb
73
+ - lib/zipper/fileEntryType.rb
74
+ - lib/zipper/jsonConfigKey.rb
75
+ - lib/zipper/version.rb
76
+ - lib/zipper/zipEntry.rb
77
+ - zipper.gemspec
78
+ homepage: https://github.com/Le0Michine/zipper
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.6.11
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Creates zip packages with complex structure.
102
+ test_files: []