epzip 0.9.0 → 0.10.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6532a1347f807c8b3ef8a83b43defb9793ae7648eb3c350533230191c93f13ea
4
- data.tar.gz: 955f21763b1b83e0808508843d852bc222fd205ca81631fa38d545267f0153ac
3
+ metadata.gz: '009a0b3b3a3634ddfbbbb46202c0c2feb057d6fefd32843adcbc1680961e2c95'
4
+ data.tar.gz: d98f754594382b1da8e395956fb0b8ef70d6dddd4ae759fc291cfcf918f4597c
5
5
  SHA512:
6
- metadata.gz: 2e2a63c218531a117aac7c423f2e7aeb99fd76ba513cb86d8288986b77ea80016f8fd389b6cbcffb1dba544749a42f3154b36555c7806da4ff175986858bade8
7
- data.tar.gz: 52861041f0025cb7f9f6d68cb6ddb34c81f93930287e6290b1517129f2eac3919c22833480adf6626b6401c4f8d5ead8e1da522a0edaf3e3b88a6aa219d586fa
6
+ metadata.gz: c1ba6145ffa9b3f88062da2125f331ac4842a78d706677ef6482ee82c1073b7ba0b6679ba7f35bc70583ff2486742ca61321530bd84c3a5be0c7bf8459cd43f9
7
+ data.tar.gz: 8c3dbcffbdcb720d9bf9896a624de78eda3a804125836558ad64287fed0733785ad841826df62e6958e1c4a1b94f935c6f6361bab1a6d194fdd88728d38b439d
data/CHANGELOG.md ADDED
@@ -0,0 +1,44 @@
1
+ # Changelog
2
+
3
+ ## 0.10.0 - 2026-11-20
4
+
5
+ - Support rubyzip 3.x in addition to 2.x (rubyzip 2.3 or later is now required).
6
+ - Never write an extra field on the `mimetype` entry. With rubyzip 3.x a Zip64
7
+ extra field was added, which EPUB (OCF) does not allow.
8
+ - Require Ruby 3.0 or later.
9
+ - Raise `Epzip::Error` instead of `ArgumentError` for archive entries that
10
+ would extract outside the target directory.
11
+ - `Epzip.unzip` and `epunzip` no longer overwrite existing files. Pass
12
+ `overwrite: true` to `Epzip.unzip` to replace them.
13
+ - `Epzip.unzip` and `epunzip` refuse to extract through a symbolic link that
14
+ already exists in the target directory.
15
+ - `Epzip.unzip` and `epunzip` check every entry before extracting, so a
16
+ rejected archive leaves nothing behind.
17
+ - `Epzip.zip` and `epzip` refuse a symbolic link to a file outside the source
18
+ directory. Links to files inside it are still followed.
19
+ - `Epzip.unzip` and `epunzip` also reject, before extracting, an archive whose
20
+ entries conflict with each other (a name used as both a file and a
21
+ directory) or with a file or directory that already exists in the target.
22
+ - `Epzip.unzip` and `epunzip` extract entries with non-ASCII names into a
23
+ target directory with non-ASCII characters in its path (it raised
24
+ `Encoding::CompatibilityError`), and raise `Epzip::Error` for an entry name
25
+ with a null byte, for a target directory below a file, and, with
26
+ `overwrite: true`, for a named pipe or a device in place of a file.
27
+ - `Epzip.zip` no longer changes the current directory of the process, and
28
+ raises `ArgumentError` when the source is not a directory.
29
+ - `Epzip.zip` and `epzip` name the default output `book.epub` for the source
30
+ `book/` (it was `book/.epub`), and do not pack an output file left in the
31
+ source directory by an earlier run into itself.
32
+ - Print an error message instead of a backtrace when the command-line tools
33
+ hit an invalid archive.
34
+ - Do not leave an empty target directory behind when the archive cannot be
35
+ opened.
36
+ - Move the executables from `bin/` to `exe/`, and stop shipping tests and CI
37
+ settings in the gem.
38
+ - Convert README to Markdown.
39
+
40
+ ## 0.9.0 - 2026-07-04
41
+
42
+ - Support current Ruby and rubyzip versions.
43
+ - Reject archive entries that would extract outside the target directory.
44
+ - Exit with a non-zero status when the command-line tools fail.
data/README.md ADDED
@@ -0,0 +1,75 @@
1
+ # epzip
2
+
3
+ epzip (and epunzip) is an EPUB packing tool. It's just only doing 'zip' and 'unzip'. :)
4
+
5
+ ## Installation
6
+
7
+ ```console
8
+ $ gem install epzip
9
+ ```
10
+
11
+ epzip requires Ruby 3.0 or later.
12
+
13
+ ## Usage
14
+
15
+ ```console
16
+ $ epzip <srcdir> [filename]
17
+ ```
18
+
19
+ `srcdir` is the EPUB resource directory; it has mimetype, META-INF/container.xml,
20
+ and other assets.
21
+
22
+ `filename` is the EPUB filename that will be created. If no filename is given,
23
+ `srcdir` + ".epub" is used.
24
+
25
+ ```console
26
+ $ epunzip <filename> [targetdir]
27
+ ```
28
+
29
+ `filename` is the EPUB filename that will be extracted.
30
+
31
+ `targetdir` is the EPUB resource directory to be extracted into. If no targetdir
32
+ is given, the current directory is used.
33
+
34
+ You can also use it as a library:
35
+
36
+ ```ruby
37
+ require 'epzip'
38
+
39
+ Epzip.zip("book", "book.epub")
40
+ Epzip.unzip("book.epub", "book")
41
+ Epzip.unzip("book.epub", "book", overwrite: true) # replace existing files
42
+ ```
43
+
44
+ ## Handling files from someone else
45
+
46
+ epzip and epunzip stop with an error, without writing anything, when:
47
+
48
+ * `srcdir` contains a symbolic link to a file outside of it (epzip).
49
+ * an entry would be extracted outside of `targetdir`, through a symbolic link
50
+ that already exists in it, or over an existing file (epunzip).
51
+
52
+ epunzip does not limit the size or the number of the extracted files. If you
53
+ extract EPUB files that you cannot trust, check them first; for example, sum
54
+ up `Zip::Entry#size` of the entries with rubyzip.
55
+
56
+ ## Development
57
+
58
+ ```console
59
+ $ bundle install
60
+ $ bundle exec rake test
61
+ ```
62
+
63
+ ## Note on Patches/Pull Requests
64
+
65
+ * Fork the project.
66
+ * Make your feature addition or bug fix.
67
+ * Add tests for it. This is important so I don't break it in a
68
+ future version unintentionally.
69
+ * Commit, do not mess with rakefile, version, or history.
70
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
71
+ * Send me a pull request. Bonus points for topic branches.
72
+
73
+ ## Copyright
74
+
75
+ Copyright (c) 2010 Masayoshi Takahashi. See LICENSE for details.
data/exe/epunzip ADDED
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'epzip'
5
+
6
+ def error_and_exit(msg)
7
+ warn msg
8
+ exit 1
9
+ end
10
+
11
+ def usage
12
+ error_and_exit "usage: epunzip <filename> [targetdir]"
13
+ end
14
+
15
+ usage unless ARGV.size == 1 || ARGV.size == 2
16
+
17
+ file = ARGV.shift
18
+ unless File.exist?(file)
19
+ error_and_exit "No such file: #{file}"
20
+ end
21
+
22
+ dir = ARGV.shift
23
+ if dir && File.exist?(dir)
24
+ error_and_exit "Directory already exists: #{dir}"
25
+ end
26
+
27
+ begin
28
+ Epzip.unzip(file, dir)
29
+ rescue Epzip::Error, Zip::Error => e
30
+ error_and_exit e.message
31
+ end
data/exe/epzip ADDED
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'epzip'
5
+
6
+ def error_and_exit(msg)
7
+ warn msg
8
+ exit 1
9
+ end
10
+
11
+ def usage
12
+ error_and_exit "usage: epzip <srcdir> [filename]"
13
+ end
14
+
15
+ usage unless ARGV.size == 1 || ARGV.size == 2
16
+
17
+ dir = ARGV.shift
18
+ unless File.directory?(dir)
19
+ error_and_exit "No such directory: #{dir}"
20
+ end
21
+
22
+ file = ARGV.shift
23
+ if file && File.exist?(file)
24
+ error_and_exit "File already exists: #{file}"
25
+ end
26
+
27
+ begin
28
+ Epzip.zip(dir, file)
29
+ rescue Epzip::Error, Zip::Error => e
30
+ error_and_exit e.message
31
+ end
data/lib/epzip/version.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Epzip
2
- VERSION = "0.9.0"
4
+ VERSION = "0.10.0"
3
5
  end
data/lib/epzip.rb CHANGED
@@ -1,60 +1,242 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'zip'
2
4
  require 'fileutils'
3
- require 'epzip/version'
5
+ require_relative 'epzip/version'
4
6
 
5
7
  class Epzip
6
- MIMETYPE_FILENAME = 'mimetype'.freeze
7
- MIMETYPE_CONTENT = 'application/epub+zip'.freeze
8
+ class Error < StandardError; end
9
+
10
+ MIMETYPE_FILENAME = 'mimetype'
11
+ MIMETYPE_CONTENT = 'application/epub+zip'
8
12
 
13
+ # Packs the directory +epubdir+ into +epubfile+ (default: epubdir + ".epub").
14
+ #
15
+ # Raises Epzip::Error if +epubdir+ contains a symbolic link to a file outside
16
+ # of it, so that packing a directory someone else prepared cannot pull in
17
+ # unrelated files.
9
18
  def self.zip(epubdir, epubfile = nil)
10
- unless File.exist?(epubdir)
11
- raise ArgumentError, "No such directory -- #{epubdir}"
12
- end
19
+ Packer.new(epubdir, epubfile).pack
20
+ end
13
21
 
14
- epubfile ||= epubdir + ".epub"
22
+ # Extracts +epubfile+ into +epubdir+ (default: the current directory).
23
+ #
24
+ # Every entry is checked before anything is written. Epzip::Error is raised,
25
+ # and nothing is extracted, if an entry would be written outside of
26
+ # +epubdir+, through a symbolic link that already exists in it, or over an
27
+ # existing file. Pass <tt>overwrite: true</tt> to replace existing files.
28
+ def self.unzip(epubfile, epubdir = nil, overwrite: false)
29
+ Unpacker.new(epubfile, epubdir, overwrite: overwrite).unpack
30
+ end
15
31
 
16
- Zip::OutputStream.open(epubfile) do |zos|
17
- # The mimetype entry must come first and be stored uncompressed.
18
- zos.put_next_entry(MIMETYPE_FILENAME, nil, nil, Zip::Entry::STORED)
19
- zos << MIMETYPE_CONTENT
32
+ class Packer # :nodoc:
33
+ # File::ALT_SEPARATOR is the backslash on Windows, and nil elsewhere.
34
+ TRAILING_SEPARATORS = /[#{Regexp.escape([File::SEPARATOR, File::ALT_SEPARATOR].join)}]+\z/
20
35
 
21
- Dir.chdir(epubdir) do
22
- Dir.glob("**/*") do |path|
23
- next if path == MIMETYPE_FILENAME
24
- next unless File.file?(path)
36
+ def initialize(epubdir, epubfile)
37
+ unless File.directory?(epubdir)
38
+ raise ArgumentError, "No such directory -- #{epubdir}"
39
+ end
25
40
 
41
+ @epubdir = epubdir
42
+ # "book/" must become "book.epub", not "book/.epub".
43
+ @epubfile = epubfile || epubdir.to_s.sub(TRAILING_SEPARATORS, "") + ".epub"
44
+ end
45
+
46
+ def pack
47
+ # List the files before creating the output, so that a rejected source
48
+ # directory does not leave a partial EPUB behind.
49
+ paths = source_paths
50
+
51
+ Zip::OutputStream.open(@epubfile) do |zos|
52
+ # The mimetype entry must come first and be stored uncompressed.
53
+ zos.put_next_entry(mimetype_entry, nil, nil, Zip::Entry::STORED)
54
+ zos << MIMETYPE_CONTENT
55
+
56
+ paths.each do |path|
26
57
  zos.put_next_entry(path)
27
- zos << File.binread(path)
58
+ zos << File.binread(File.join(@epubdir, path))
59
+ end
60
+ end
61
+
62
+ @epubfile
63
+ end
64
+
65
+ private
66
+
67
+ # Returns the files to pack, as paths relative to the source directory.
68
+ def source_paths
69
+ root = File.realpath(@epubdir)
70
+ # Only a root directory ends with a separator.
71
+ prefix = root.end_with?(File::SEPARATOR) ? root : root + File::SEPARATOR
72
+ # An EPUB left in the source directory by an earlier run is about to be
73
+ # replaced, so it cannot be packed into itself.
74
+ output = File.realpath(@epubfile) if File.exist?(@epubfile)
75
+
76
+ Dir.glob("**/*", base: @epubdir).select do |path|
77
+ next false if path == MIMETYPE_FILENAME
78
+
79
+ file = File.join(@epubdir, path)
80
+ # File.file? follows symbolic links, and so would reading the file.
81
+ next false unless File.file?(file)
82
+
83
+ realpath = File.realpath(file)
84
+ unless realpath.start_with?(prefix)
85
+ raise Error, "Symbolic link to outside of the source directory -- #{path}"
28
86
  end
87
+
88
+ realpath != output
29
89
  end
30
90
  end
31
91
 
32
- epubfile
92
+ # EPUB (OCF) forbids an extra field on the mimetype entry. rubyzip 3.x adds
93
+ # a Zip64 placeholder to every streamed entry whose size is not yet known,
94
+ # so declare the size up front.
95
+ def mimetype_entry
96
+ entry = Zip::Entry.new(@epubfile, MIMETYPE_FILENAME)
97
+ entry.compression_method = Zip::Entry::STORED
98
+ entry.size = MIMETYPE_CONTENT.bytesize
99
+ entry
100
+ end
33
101
  end
102
+ private_constant :Packer
34
103
 
35
- def self.unzip(epubfile, epubdir = nil)
36
- epubdir ||= Dir.pwd
37
- FileUtils.mkdir_p(epubdir)
104
+ class Unpacker # :nodoc:
105
+ # Zip::Entry#extract takes the destination path in rubyzip 2.x, and an
106
+ # entry path relative to +destination_directory+ in rubyzip 3.x.
107
+ RUBYZIP_V3_EXTRACT = Zip::Entry.instance_method(:extract).parameters
108
+ .any? { |_, name| name == :destination_directory }
38
109
 
39
- root = File.expand_path(epubdir)
110
+ def initialize(epubfile, epubdir, overwrite:)
111
+ @epubfile = epubfile
112
+ @epubdir = epubdir || Dir.pwd
113
+ @root = File.expand_path(@epubdir)
114
+ # Only a root directory ends with a separator.
115
+ @prefix = @root.end_with?(File::SEPARATOR) ? @root : @root + File::SEPARATOR
116
+ @overwrite = overwrite
117
+ end
40
118
 
41
- Zip::File.open(epubfile) do |zip|
42
- zip.each do |entry|
43
- next if entry.name_is_directory?
119
+ def unpack
120
+ Zip::File.open(@epubfile) do |zip|
121
+ check_root
122
+ # A directory entry usually ends with a separator, but may also be
123
+ # marked as a directory by its attributes alone.
124
+ files = zip.reject { |entry| entry.name_is_directory? || entry.directory? }
125
+ targets = files.map { |entry| [entry, target_path(entry)] }
126
+ check_conflicts(targets)
44
127
 
45
- filepath = File.expand_path(File.join(epubdir, entry.name))
46
- # Guard against Zip Slip: rubyzip skips its own name check when an
47
- # explicit destination path is given, so reject entries that would
48
- # escape the target directory.
49
- unless filepath == root || filepath.start_with?(root + File::SEPARATOR)
50
- raise ArgumentError, "Illegal entry name -- #{entry.name}"
128
+ # Create the target only once the archive has been opened and checked,
129
+ # so that a rejected file does not leave an empty directory behind.
130
+ FileUtils.mkdir_p(@root)
131
+ targets.each do |entry, filepath|
132
+ FileUtils.mkdir_p(File.dirname(filepath))
133
+ extract(entry, filepath)
51
134
  end
135
+ end
136
+
137
+ @epubdir
138
+ end
139
+
140
+ private
52
141
 
53
- FileUtils.mkdir_p(File.dirname(filepath))
54
- entry.extract(filepath) { true }
142
+ # Raises Epzip::Error if the target directory cannot be created or used,
143
+ # because something else (or a dangling symbolic link) has its name or
144
+ # the name of one of the directories leading to it.
145
+ def check_root
146
+ # The target directory itself, or the closest thing that exists above it.
147
+ path = @root
148
+ until File.exist?(path) || File.symlink?(path)
149
+ parent = File.dirname(path)
150
+ break if parent == path
151
+
152
+ path = parent
55
153
  end
154
+ return if File.directory?(path)
155
+
156
+ raise Error, "Not a directory -- #{path}"
56
157
  end
57
158
 
58
- epubdir
159
+ # Returns the path +entry+ is to be extracted to, or raises Epzip::Error if
160
+ # it must not be extracted.
161
+ def target_path(entry)
162
+ # rubyzip returns the name as a binary string, which cannot be joined to
163
+ # a target directory with non-ASCII characters in its path.
164
+ name = entry.name.dup.force_encoding(@root.encoding)
165
+ filepath = begin
166
+ File.expand_path(File.join(@root, name))
167
+ rescue ArgumentError # a null byte in the name
168
+ raise Error, "Illegal entry name -- #{entry.name.inspect}"
169
+ end
170
+ # Guard against Zip Slip: reject entries that would escape the target
171
+ # directory instead of relying on rubyzip, which either skips its own
172
+ # check (2.x, explicit destination path) or only warns (3.x).
173
+ unless filepath.start_with?(@prefix)
174
+ raise Error, "Illegal entry name -- #{entry.name}"
175
+ end
176
+
177
+ # The check above only looks at the path as a string. A symbolic link
178
+ # below the target directory could still lead the write elsewhere.
179
+ path = filepath
180
+ until path == @root
181
+ if File.symlink?(path)
182
+ raise Error, "Symbolic link in the target directory -- #{path}"
183
+ end
184
+ # A directory is needed here, and a file cannot be replaced by one.
185
+ if path != filepath && File.exist?(path) && !File.directory?(path)
186
+ raise Error, "Not a directory -- #{path}"
187
+ end
188
+
189
+ path = File.dirname(path)
190
+ end
191
+
192
+ # A directory cannot be replaced by a file, even when overwriting.
193
+ if File.directory?(filepath)
194
+ raise Error, "Directory already exists -- #{filepath}"
195
+ elsif File.exist?(filepath)
196
+ raise Error, "File already exists -- #{filepath}" unless @overwrite
197
+ # Writing to a named pipe or a device would not replace it.
198
+ raise Error, "Not a regular file -- #{filepath}" unless File.file?(filepath)
199
+ end
200
+
201
+ filepath
202
+ end
203
+
204
+ # Raises Epzip::Error if the entries cannot all be extracted: two of them
205
+ # go to the same path, or one is a file where another needs a directory.
206
+ # Such an archive would otherwise fail halfway through the extraction.
207
+ def check_conflicts(targets)
208
+ entries = {}
209
+ targets.each do |entry, filepath|
210
+ if entries.key?(filepath)
211
+ raise Error, "Duplicate entry -- #{entry.name}"
212
+ end
213
+
214
+ entries[filepath] = entry
215
+ end
216
+
217
+ targets.each do |_entry, filepath|
218
+ dir = File.dirname(filepath)
219
+ until dir == @root
220
+ if entries.key?(dir)
221
+ raise Error, "Entry is both a file and a directory -- #{entries[dir].name}"
222
+ end
223
+
224
+ dir = File.dirname(dir)
225
+ end
226
+ end
227
+ end
228
+
229
+ # The block tells rubyzip whether it may replace an existing file.
230
+ def extract(entry, filepath)
231
+ if RUBYZIP_V3_EXTRACT
232
+ # The checked path, relative to the target directory, and not the
233
+ # binary entry name: rubyzip joins it to the directory again.
234
+ relative = filepath.delete_prefix(@prefix)
235
+ entry.extract(relative, destination_directory: @root) { @overwrite }
236
+ else
237
+ entry.extract(filepath) { @overwrite }
238
+ end
239
+ end
59
240
  end
241
+ private_constant :Unpacker
60
242
  end
metadata CHANGED
@@ -1,11 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: epzip
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masayoshi Takahashi
8
- bindir: bin
8
+ bindir: exe
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
@@ -15,20 +15,20 @@ dependencies:
15
15
  requirements:
16
16
  - - ">="
17
17
  - !ruby/object:Gem::Version
18
- version: '1.0'
18
+ version: '2.3'
19
19
  - - "<"
20
20
  - !ruby/object:Gem::Version
21
- version: '3.0'
21
+ version: '4.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  requirements:
26
26
  - - ">="
27
27
  - !ruby/object:Gem::Version
28
- version: '1.0'
28
+ version: '2.3'
29
29
  - - "<"
30
30
  - !ruby/object:Gem::Version
31
- version: '3.0'
31
+ version: '4.0'
32
32
  description: epzip is an EPUB packing tool. It's just only to do 'zip.'
33
33
  email: takahashimm@gmail.com
34
34
  executables:
@@ -37,24 +37,18 @@ executables:
37
37
  extensions: []
38
38
  extra_rdoc_files: []
39
39
  files:
40
- - ".github/dependabot.yml"
41
- - ".github/workflows/ruby.yml"
42
- - ".gitignore"
43
- - Gemfile
40
+ - CHANGELOG.md
44
41
  - LICENSE
45
- - README.rdoc
46
- - Rakefile
47
- - bin/epunzip
48
- - bin/epzip
49
- - epzip.gemspec
42
+ - README.md
43
+ - exe/epunzip
44
+ - exe/epzip
50
45
  - lib/epzip.rb
51
46
  - lib/epzip/version.rb
52
- - test/helper.rb
53
- - test/test_epzip.rb
54
47
  homepage: https://github.com/takahashim/epzip
55
48
  licenses:
56
49
  - MIT
57
- metadata: {}
50
+ metadata:
51
+ changelog_uri: https://github.com/takahashim/epzip/blob/master/CHANGELOG.md
58
52
  rdoc_options: []
59
53
  require_paths:
60
54
  - lib
@@ -62,14 +56,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
62
56
  requirements:
63
57
  - - ">="
64
58
  - !ruby/object:Gem::Version
65
- version: '2.6'
59
+ version: '3.0'
66
60
  required_rubygems_version: !ruby/object:Gem::Requirement
67
61
  requirements:
68
62
  - - ">="
69
63
  - !ruby/object:Gem::Version
70
64
  version: '0'
71
65
  requirements: []
72
- rubygems_version: 4.0.10
66
+ rubygems_version: 3.6.9
73
67
  specification_version: 4
74
68
  summary: simple EPUB packing tool
75
69
  test_files: []
@@ -1,8 +0,0 @@
1
- version: 2
2
- updates:
3
- - package-ecosystem: bundler
4
- directory: "/"
5
- schedule:
6
- interval: daily
7
- time: "20:00"
8
- open-pull-requests-limit: 10
@@ -1,37 +0,0 @@
1
- # This workflow uses actions that are not certified by GitHub.
2
- # They are provided by a third-party and are governed by
3
- # separate terms of service, privacy policy, and support
4
- # documentation.
5
- # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
- # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
-
8
- name: Ruby
9
-
10
- on:
11
- push:
12
- branches: [ "master" ]
13
- pull_request:
14
- branches: [ "master" ]
15
-
16
- permissions:
17
- contents: read
18
-
19
- jobs:
20
- test:
21
-
22
- runs-on: ubuntu-latest
23
- strategy:
24
- matrix:
25
- ruby-version: ['3.0', '3.1', '3.2', '3.3', '3.4', '4.0']
26
-
27
- steps:
28
- - uses: actions/checkout@v7
29
- - name: Set up Ruby
30
- # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
31
- # change this to (see https://github.com/ruby/setup-ruby#versioning):
32
- uses: ruby/setup-ruby@v1
33
- with:
34
- ruby-version: ${{ matrix.ruby-version }}
35
- bundler-cache: true # runs 'bundle install' and caches installed gems automatically
36
- - name: Run tests
37
- run: bundle exec rake
data/.gitignore DELETED
@@ -1,23 +0,0 @@
1
- ## MAC OS
2
- .DS_Store
3
-
4
- ## TEXTMATE
5
- *.tmproj
6
- tmtags
7
-
8
- ## EMACS
9
- *~
10
- \#*
11
- .\#*
12
-
13
- ## VIM
14
- *.swp
15
-
16
- ## PROJECT::GENERAL
17
- coverage
18
- rdoc
19
- pkg
20
- *.gem
21
- /Gemfile.lock
22
-
23
- ## PROJECT::SPECIFIC
data/Gemfile DELETED
@@ -1,9 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- # Runtime dependencies are declared in epzip.gemspec.
4
- gemspec
5
-
6
- group :development, :test do
7
- gem 'rake'
8
- gem 'test-unit'
9
- end
data/README.rdoc DELETED
@@ -1,36 +0,0 @@
1
- = epzip
2
-
3
- epzip(eunzip) is EPUB packing tool. It's just only doing 'zip' and 'unzip'. :)
4
-
5
- == usage
6
-
7
- $ epzip <srcdir> [filename]
8
-
9
- 'srcdir' is EPUB resource directory; it has mimetype, META-INF/container.xml,
10
- and other assets.
11
-
12
- 'filename' is EPUB filename that will be created. If no filename,
13
- dir + ".epub" is used.
14
-
15
- $ epunzip <filename> [targetdir]
16
-
17
- 'filename' is EPUB filename that will be extracted.
18
-
19
- 'targetdir' is EPUB resource directory to be extracted into. If no targetdir,
20
- current directory is used.
21
-
22
-
23
-
24
- == Note on Patches/Pull Requests
25
-
26
- * Fork the project.
27
- * Make your feature addition or bug fix.
28
- * Add tests for it. This is important so I don't break it in a
29
- future version unintentionally.
30
- * Commit, do not mess with rakefile, version, or history.
31
- (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
32
- * Send me a pull request. Bonus points for topic branches.
33
-
34
- == Copyright
35
-
36
- Copyright (c) 2010 Masayoshi Takahashi. See LICENSE for details.
data/Rakefile DELETED
@@ -1,10 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require 'rake/testtask'
3
-
4
- Rake::TestTask.new(:test) do |t|
5
- t.libs << 'lib' << 'test'
6
- t.pattern = 'test/**/test_*.rb'
7
- t.verbose = true
8
- end
9
-
10
- task default: :test
data/bin/epunzip DELETED
@@ -1,28 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'epzip'
4
-
5
- def error_and_exit(msg)
6
- warn msg
7
- exit 1
8
- end
9
-
10
- def usage()
11
- error_and_exit "usage: epunzip <filename> [targetdir]"
12
- end
13
-
14
- if !(ARGV.size == 1 or ARGV.size == 2)
15
- usage
16
- end
17
-
18
- file = ARGV.shift
19
- if !File.exist? file
20
- error_and_exit "No such file: #{file}"
21
- end
22
-
23
- dir = ARGV.shift
24
- if dir and File.exist? dir
25
- error_and_exit "Directory already exists: #{dir}"
26
- end
27
-
28
- Epzip.unzip(file, dir)
data/bin/epzip DELETED
@@ -1,28 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'epzip'
4
-
5
- def error_and_exit(msg)
6
- warn msg
7
- exit 1
8
- end
9
-
10
- def usage()
11
- error_and_exit "usage: epzip <srcdir> [filename]"
12
- end
13
-
14
- if !(ARGV.size == 1 or ARGV.size == 2)
15
- usage
16
- end
17
-
18
- dir = ARGV.shift
19
- if !File.exist? dir
20
- error_and_exit "No such directory: #{dir}"
21
- end
22
-
23
- file = ARGV.shift
24
- if file and File.exist? file
25
- error_and_exit "File already exists: #{file}"
26
- end
27
-
28
- Epzip.zip(dir, file)
data/epzip.gemspec DELETED
@@ -1,22 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
-
3
- require_relative "lib/epzip/version"
4
-
5
- Gem::Specification.new do |s|
6
- s.name = "epzip"
7
- s.version = Epzip::VERSION
8
- s.authors = ["Masayoshi Takahashi"]
9
- s.email = "takahashimm@gmail.com"
10
- s.homepage = "https://github.com/takahashim/epzip"
11
- s.summary = "simple EPUB packing tool"
12
- s.description = "epzip is an EPUB packing tool. It's just only to do 'zip.'"
13
- s.license = "MIT"
14
-
15
- s.required_ruby_version = ">= 2.6"
16
-
17
- s.files = `git ls-files -z`.split("\x0")
18
- s.executables = ["epunzip", "epzip"]
19
- s.require_paths = ["lib"]
20
-
21
- s.add_runtime_dependency "rubyzip", ">= 1.0", "< 3.0"
22
- end
data/test/helper.rb DELETED
@@ -1,2 +0,0 @@
1
- require 'test/unit'
2
- require 'epzip'
data/test/test_epzip.rb DELETED
@@ -1,70 +0,0 @@
1
- require 'helper'
2
- require 'tmpdir'
3
- require 'fileutils'
4
-
5
- class TestEpzip < Test::Unit::TestCase
6
- def test_zip_should_use_existed_dir
7
- assert_raise ArgumentError do
8
- Epzip.zip("unknown_dir")
9
- end
10
- end
11
-
12
- def test_unzip_rejects_path_traversal_entry
13
- Dir.mktmpdir do |base|
14
- # Craft an archive with an entry that escapes the target directory.
15
- evil = File.join(base, "evil.zip")
16
- Zip::File.open(evil, Zip::File::CREATE) do |zip|
17
- zip.get_output_stream("../escaped.txt") { |os| os.write("pwned") }
18
- end
19
-
20
- dest = File.join(base, "out")
21
- assert_raise ArgumentError do
22
- Epzip.unzip(evil, dest)
23
- end
24
- assert !File.exist?(File.join(base, "escaped.txt")), "must not write outside dest"
25
- end
26
- end
27
-
28
- def test_zip_and_unzip_roundtrip
29
- Dir.mktmpdir do |base|
30
- src = File.join(base, "book")
31
- FileUtils.mkdir_p(File.join(src, "META-INF"))
32
- FileUtils.mkdir_p(File.join(src, "OEBPS"))
33
- File.write(File.join(src, "META-INF", "container.xml"), "<container/>\n")
34
- File.write(File.join(src, "OEBPS", "content.opf"), "<package/>\n")
35
- File.binwrite(File.join(src, "OEBPS", "img.bin"), (0..255).to_a.pack("C*"))
36
-
37
- epub = File.join(base, "out.epub")
38
- assert_equal epub, Epzip.zip(src, epub)
39
- assert File.exist?(epub)
40
-
41
- dest = File.join(base, "unpacked")
42
- Epzip.unzip(epub, dest)
43
-
44
- %w[META-INF/container.xml OEBPS/content.opf OEBPS/img.bin].each do |rel|
45
- assert File.exist?(File.join(dest, rel)), "#{rel} extracted"
46
- assert_equal File.binread(File.join(src, rel)),
47
- File.binread(File.join(dest, rel)),
48
- "#{rel} content matches"
49
- end
50
- end
51
- end
52
-
53
- def test_zip_stores_mimetype_first_and_uncompressed
54
- Dir.mktmpdir do |base|
55
- src = File.join(base, "book")
56
- FileUtils.mkdir_p(File.join(src, "OEBPS"))
57
- File.write(File.join(src, "OEBPS", "content.opf"), "<package/>\n")
58
-
59
- epub = File.join(base, "out.epub")
60
- Epzip.zip(src, epub)
61
-
62
- Zip::File.open(epub) do |zip|
63
- first = zip.entries.first
64
- assert_equal Epzip::MIMETYPE_FILENAME, first.name
65
- assert_equal Zip::Entry::STORED, first.compression_method
66
- assert_equal Epzip::MIMETYPE_CONTENT, first.get_input_stream.read
67
- end
68
- end
69
- end
70
- end