Tardotgz is an extraction of a few archive utility methods I wrote while working on a project at GitHub. These methods simplify the creation of gzipped tarball archives and the reading and extraction of files from those archives.
One of the unique features of the #read_from_archive and #extract_from_archive utility methods is the ability to pass them a block (in fact #extract_from_archive
just calls #read_from_archive
and passes it a block).
And in case you were wondering, I've been pronouncing it "tar-dot-jeez" 😁
You can install at the command line:
$ gem install tardotgz
Or add the gem to your Gemfile:
source "https://rubygems.org"
gem "tardotgz"
Start by including the module into the class where you'll use it.
require "tardotgz"
class FruitRollup
include Tardotgz
end
Create an archive from a folder by calling #create_archive
with the source path (the directory or file you want to archive) and the target path (the path and filename for the archive that it creates).
> fr = FruitRollup.new
=> #<FruitRollup:0x007fc0e4acf538>
> source_path = File.expand_path("~/Notes")
=> "/Users/jonmagic/Notes"
> archive_path = File.expand_path("~/Dropbox/notes.tar.gz")
=> "/Users/jonmagic/Dropbox/notes.tar.gz"
> fr.create_archive(source_path, archive_path)
=> "/Users/jonmagic/Dropbox/notes.tar.gz"
You can read the contents of a file (or files) in an archive without unzipping or untarring the archive by using the #read_from_archive
method. It's use is also fairly flexible.
> fr.read_from_archive(archive_path, "testing1.md")
=> "hello world\n"
> fr.read_from_archive(archive_path, /testing\d.md/) # read all files matching pattern
=> "hello world\nfoobarbaz\n"
> fr.read_from_archive(archive_path, /testing\d.md/) do |tarfile| # pass a block
* puts tarfile.read.upcase
* end
HELLO WORLD
FOOBARBAZ
=> nil
You can extract the contents of an archive with #extract_from_archive.
> destination_path = File.expand_path("~/Restored notes")
=> "/Users/jonmagic/Restored notes"
> fr.extract_from_archive(archive_path, destination_path, "testing1.md")
=> "/Users/jonmagic/Restored notes"
> File.exist?("#{destination_path}/testing1.md")
=> true
> File.exist?("#{destination_path}/testing2.md")
=> false
> fr.extract_from_archive(archive_path, destination_path, /testing\d\.md/)
=> "/Users/jonmagic/Restored notes"
> File.exist?("#{destination_path}/testing1.md")
=> true
> File.exist?("#{destination_path}/testing2.md")
=> true
> fr.extract_from_archive(archive_path, destination_path, /testing\d\.md/) do
* puts File.exist?("#{destination_path}/testing1.md")
* puts File.exist?("#{destination_path}/testing2.md")
* end
true
true
=> nil
> File.exist?(destination_path)
=> false
If you'd like to hack on Tardotgz, start by forking the repo on GitHub:
https://github.com/jonmagic/tardotgz
The best way to get your changes merged back into core is as follows:
- Clone down your fork
- Create a thoughtfully named topic branch to contain your change
- Hack away
- If you are adding new functionality, document it in the README
- If necessary, rebase your commits into logical chunks, without errors
- Push the branch up to GitHub
- Send a pull request for your branch
The MIT License (MIT)
Copyright (c) 2015 Jonathan Hoyt
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.