Giter Site home page Giter Site logo

libarchive-ruby's Introduction

libarchive-ruby

This is libarchive-ruby, your preferred archiving toolkit in Ruby! libarchive-ruby is a Ruby binding to the famous libarchive library and supports nearly all features the library exposes through it’s C++ interface.

librarchive-ruby focuses on a clean and ruby-like syntax that makes it trivial to create, read, write and extract archive files of various formats. Want to know more? Keep reading!

Prerequesites

In order to successfully install libarchive-ruby, you need the following:

  • Ruby >= 1.8.7

  • A C++ compiler (we use the GNU project’s g++)

  • libarchive (we tested with 2.8.4)

How to install?

RubyGems

This is the easiest and preferred way. Ensure you have a proper built environment for C++ code, and then do

# gem install libarchive-ruby

Building from source

If you want to be on the bleeding etch, clone the our git repository at GitHub:

$ git clone https://github.com/Hanmac/libarchive-ruby.git
$ cd libarchive-ruby

Then you can either choose to use the library from that directory by running

$ rake compile

or to make a gem and install that one.

$ rake gem
# gem install --local pkg/libarchive-ruby-x.x.x

How to use?

First, you have to require the library:

require "archive" #Note this is NOT "libarchive-ruby"

Then you can use the beautiful rubyish API:

Read an archive

Assuming, you have “myarchive.tar.bz2” in the current directory.

a = Archive.new("myarchive.tar.bz2")
puts "This archive contains:"
a.each{|entry| puts entry}

#Archive includes the Enumerable module, making available all that 
#nice enumerating functionality: 
puts All entries in uppercase are:
puts a.map{|entry| entry.path.upcase}.join("\n")

#Furthermore, you can even read from the files contained in the 
#archive without actually extracting it:
a.each{|entry, data| puts "Content of #{entry} is: #{data}"}

Extract an archive

Assuming you have “myarchive.tar.bz2” in the current directory.

a = Archive.new("myarchive.tar.bz2")

#Extract all files to the current directory
a.extract

#Extract a specific file
a.extract("mydir/myfile")

#Restrict what file attributes are extracted:
a.extract(:extract => Archive::EXTRACT_OWNER | Archive::EXTRACT_TIME)

#Extract only files whose path is longer than 5 characters
a.extract_if{|entry| entry.path.size > 5}

Create an archive

Assuming you have three files “a.rb”, “b.cpp” and “README.rdoc” in your current directory:

a = Archive.new("myarchive.tar.gz")
a << "a.rb" << "b.cpp" << "README.rdoc"

If you have more than one file to add you can use an Array:

a = Archive.new("myarchive.tar.gz")
a << ["a.rb", "b.cpp", "README.rdoc"]

You can manipulate the entry witch is added to the file:

a = Archive.new("myarchive.tar.gz")
a.add("a.rb") {|entry| entry.mtime = Time.now }

#Note how libarchive-ruby automatically picked up the archive format
#you wanted by examining the file extension:
a = Archive.new("myarchive.tar.gz")
puts a.format_name #=> POSIX ustar format

Further reading

Have a look at the documentation for the Archive and Archive::Entry classes for more information.

Contributors

The following people have worked on libarchive-ruby beside me:

  • Quintus <sutniuq ÄT gmx DÖT net> contributed to the docs

License

libarchive-ruby is a Ruby binding for the C library libarchive.

Copyright © 2011 Hans Mackowiak

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

Contact

You can read me via the email address hanmac ÄT gmx DÖT de.

libarchive-ruby's People

Contributors

grossws avatar hanmac avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

libarchive-ruby's Issues

Unable to compile from git

I'm trying to build libarchive-ruby on an Ubuntu 12.10 64bit machine, with libarchive-dev 3.0.4 installed (using the standard system-provided packages). I've tried using Ruby 1.9.3-p392 (built with/by RVM) and the system-provided Ruby 1.9.3p194. Both fail with the same results:

rmckern@galvatron libarchive-ruby $ rake compile
cd ext
/opt/rvm/rubies/ruby-1.9.3-p392/bin/ruby extconf.rb
checking for main() in -larchive... yes
checking for archive.h... yes
checking for rb_string_value_cstr() in ruby.h... yes
checking for RETURN_ENUMERATOR in ruby.h... yes
checking for rb_proc_arity() in ruby.h... yes
checking for archive_read_support_format_raw() in archive.h... yes
creating extconf.h
creating Makefile
make
compiling main.cpp
main.cpp:26:7: error: redefinition of ‘VALUE wrap(T*) [with T = rarchive; VALUE = long unsigned int]’
In file included from main.cpp:23:0:
main.hpp:60:14: error: ‘VALUE wrap(T*) [with T = rarchive; VALUE = long unsigned int]’ previously declared here
main.cpp:33:11: error: redefinition of ‘T wrap(const VALUE&) [with T = rarchive*; VALUE = long unsigned int]’
In file included from main.cpp:23:0:
main.hpp:67:18: error: ‘T wrap(const VALUE&) [with T = rarchive*; VALUE = long unsigned int]’ previously declared here
main.cpp:42:7: error: redefinition of ‘VALUE wrap(T*) [with T = archive_entry; VALUE = long unsigned int]’
In file included from main.cpp:23:0:
main.hpp:76:14: error: ‘VALUE wrap(T*) [with T = archive_entry; VALUE = long unsigned int]’ previously declared here
main.cpp:51:16: error: redefinition of ‘T wrap(const VALUE&) [with T = archive_entry*; VALUE = long unsigned int]’
In file included from main.cpp:23:0:
main.hpp:85:23: error: ‘T wrap(const VALUE&) [with T = archive_entry*; VALUE = long unsigned int]’ previously declared here
main.cpp:60:7: error: redefinition of ‘VALUE wrap(T*) [with T = const char; VALUE = long unsigned int]’
In file included from main.cpp:23:0:
main.hpp:94:14: error: ‘VALUE wrap(T*) [with T = const char; VALUE = long unsigned int]’ previously declared here
main.cpp:65:13: error: redefinition of ‘T wrap(const VALUE&) [with T = const char*; VALUE = long unsigned int]’
In file included from main.cpp:23:0:
main.hpp:99:13: error: ‘T wrap(const VALUE&) [with T = const char*; VALUE = long unsigned int]’ previously declared here
make: *** [main.o] Error 1
rake aborted!
Command failed with status (2): [make...]
/tmp/libarchive-ruby/Rakefile.rb:78:in `block in <top (required)>'
/opt/rvm/gems/ruby-1.9.3-p392/bin/ruby_noexec_wrapper:14:in `eval'
/opt/rvm/gems/ruby-1.9.3-p392/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => compile
(See full trace by running task with --trace)

Additionally, on an OS X 10.8.3 machine with Xcode 4.6.2 (and associated command line tools), Ruby 1.9.3-p392 (from RVM), libarchive 3.1.2, and apple-gcc42 (4.2.1-5666.3, and both from Homebrew) I see this problem:

ryan@bender libarchive-ruby $ rake compile
cd ext
/usr/local/rvm/rubies/ruby-1.9.3-p392/bin/ruby extconf.rb
checking for main() in -larchive... *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
    --with-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/usr/local/rvm/rubies/ruby-1.9.3-p392/bin/ruby
    --with-archive-dir
    --without-archive-dir
    --with-archive-include
    --without-archive-include=${archive-dir}/include
    --with-archive-lib
    --without-archive-lib=${archive-dir}/lib
    --with-libarchive-config
    --without-libarchive-config
    --with-pkg-config
    --without-pkg-config
    --with-archivelib
    --without-archivelib
/usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/mkmf.rb:381:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
    from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/mkmf.rb:461:in `try_link0'
    from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/mkmf.rb:476:in `try_link'
    from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/mkmf.rb:619:in `try_func'
    from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/mkmf.rb:870:in `block in find_library'
    from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/mkmf.rb:790:in `block in checking_for'
    from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/mkmf.rb:284:in `block (2 levels) in postpone'
    from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/mkmf.rb:254:in `open'
    from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/mkmf.rb:284:in `block in postpone'
    from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/mkmf.rb:254:in `open'
    from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/mkmf.rb:280:in `postpone'
    from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/mkmf.rb:789:in `checking_for'
    from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/mkmf.rb:866:in `find_library'
    from extconf.rb:31:in `block in <main>'
    from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/mkmf.rb:535:in `with_cflags'
    from extconf.rb:29:in `<main>'
rake aborted!
Command failed with status (1): [/usr/local/rvm/rubies/ruby-1.9.3-p392/bin/...]
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/file_utils.rb:53:in `block in create_shell_runner'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/file_utils.rb:45:in `call'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/file_utils.rb:45:in `sh'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/file_utils_ext.rb:37:in `sh'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/file_utils.rb:82:in `ruby'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/file_utils_ext.rb:37:in `ruby'
/private/tmp/libarchive-ruby/Rakefile.rb:77:in `block in <top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:246:in `call'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:246:in `block in execute'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:241:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:241:in `execute'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:184:in `block in invoke_with_call_chain'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:177:in `invoke_with_call_chain'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:170:in `invoke'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:143:in `invoke_task'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:101:in `block (2 levels) in top_level'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:101:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:101:in `block in top_level'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:110:in `run_with_threads'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:95:in `top_level'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:73:in `block in run'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:160:in `standard_exception_handling'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:70:in `run'
/usr/local/rvm/gems/ruby-1.9.3-p392/bin/ruby_noexec_wrapper:14:in `eval'
/usr/local/rvm/gems/ruby-1.9.3-p392/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => compile
(See full trace by running task with --trace)

Other binary gems compile and install fine. I am totally stumped.

Is it possible to install this gem with custom path to libarchive?

Hi,
I've tried every possible environment variable and such that I can think of in an effort to add paths to the include and lib directories of the compiled libarchive-3.1.3, but to no avail. What am I doing wrong or is this simply not possible? Can't I install this gem without root access on a system without the libarchive C-library already present? Every time I try to install the gem itself it will crash as it can't find libarchive..

Extract places files where ever ruby was started

Currently if you use #extract, the extracted files go to wherever the ruby process was started. This is an issue if the script is part of a crontab or such. It would be nice if either the method assumed to extract to the same directory the archive is in or if there was an option to set a directory to extract to. Thanks for the gem by the way.

Problem installing gem

Hello,

I'm trying to install the gem using:

 gem install libarchive-ruby

using this version of Ruby:

 ruby 1.8.7 (2010-04-19 patchlevel 253) [i686-darwin10.4.0], MBARI 0x6770, Ruby Enterprise Edition 2010.02

I get:

ERROR:  Error installing libarchive-ruby:
libarchive-ruby requires Ruby version >= 1.9.

I saw on the last commit that you have changed minimum version of Ruby to 1.8.7. Maybe you forgot to push to Rubygems ?
On the docs on Rubygems the readme states that the minimum required version is 1.9 (http://rubydoc.info/gems/libarchive-ruby/0.0.2/frames)

BTW, the gem is awesome!!

Thanks,
Cristi

libarchive-ruby crashes on directories

Hi there,

The library crashes with a C++ exception if one tries to add a directory to an Archive object.

√ quintus@hades => ~/Desktop/foo
$ tree
.
└── foofoo
    └── test

1 directory, 1 file
√ quintus@hades => ~/Desktop/foo
$ cat foofoo/test 
test
√ quintus@hades => ~/Desktop/foo
$ ruby -rarchive -e 'a = Archive.new("test.tar.bz2"); a << "foofoo"'
terminate called after throwing an instance of 'std::length_error'
  what():  basic_string::append
Abgebrochen
√ quintus@hades => ~/Desktop/foo
$ 

ruby -v: ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]
gem -v: 1.8.10
OS: Arch Linux
libarchive version: 2.8.4
libarchive-ruby version: 0.0.3

I had expected to either add the directory recursively to the archive, or to at least crash with a Ruby exception saying that directories cannot be included directly. Note that appending the foofoo/test file directly works without problems:

√ quintus@hades => ~/Desktop/foo
$ rm test.tar.bz2 
√ quintus@hades => ~/Desktop/foo
$ ruby -rarchive -e 'a = Archive.new("test.tar.bz2"); a << "foofoo/test"'
√ quintus@hades => ~/Desktop/foo
$ tar -tjf test.tar.bz2 
foofoo/test

Valete,
Marvin

Error when requiring gem with ruby 1.8.7-p302

Installing

$ gem install libarchive-ruby
Fetching: libarchive-ruby-0.0.3.gem (100%)
Building native extensions.  This could take a while...
Successfully installed libarchive-ruby-0.0.3
1 gem installed
Installing ri documentation for libarchive-ruby-0.0.3...
Installing RDoc documentation for libarchive-ruby-0.0.3...

Running

$ irb
ruby-1.8.7-p302 :001 > require 'rubygems'
 => true 
ruby-1.8.7-p302 :002 > require 'archive'
LoadError: /home/bedlamp/.rvm/gems/ruby-1.8.7-p302/gems/libarchive-ruby-0.0.3/lib/archive.so: undefined symbol: _ZNSs4_Rep20_S_empty_rep_storageE - /home/bedlamp/.rvm/gems/ruby-1.8.7-p302/gems/libarchive-ruby-0.0.3/lib/archive.so
    from /home/bedlamp/.rvm/gems/ruby-1.8.7-p302/gems/libarchive-ruby-0.0.3/lib/archive.so
    from /home/bedlamp/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:59:in `require'
    from (irb):2

Ubuntu 10.10
libarchive version 2.8.4-1

terminate called after throwing an instance of 'std::length_error'

terminate called after throwing an instance of 'std::length_error'
what(): basic_string::append
Aborted

How to reproduce:

require 'archive'
archive = Archive.new('test.tar.xz')
files_to_add = Dir['*/']

The above works

now add it

archive << files_to_add

^^^ leads to the above error; perhaps the error message can be improved as well?

Manual installation of the library under Ruby 1.8.7 Enterprise Edition

Hello,

I'm trying to compile the library manually. When I try rake compile, this is the result I get:

➜  libarchive-ruby git:(master) rake compile
(in /Users/cristi/Downloads/libarchive-ruby)
cd ext
/Users/cristi/.rvm/rubies/ree-1.8.7-2010.02/bin/ruby extconf.rb
checking for main() in -larchive... yes
checking for archive.h... yes
checking for rb_string_value_cstr() in ruby.h... yes
checking for RETURN_ENUMERATOR in ruby.h... yes
creating Makefile
make
g++ -I. -I. -I/Users/cristi/.rvm/rubies/ree-1.8.7-2010.02/lib/ruby/1.8/i686-darwin10.4.0 -I. -DHAVE_RB_STRING_VALUE_CSTR  -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE   -fno-common -g -O2  -pipe -fno-common  -Wall   -c archive.cpp
archive.cpp: In function ‘VALUE Archive_each_block(archive*)’:
archive.cpp:378: error: ‘rb_proc_arity’ was not declared in this scope
archive.cpp: In function ‘VALUE Archive_map_block(write_obj*)’:
archive.cpp:568: error: ‘rb_proc_arity’ was not declared in this scope
make: *** [archive.o] Error 1
rake aborted!
Command failed with status (2): [make...]
/Users/cristi/Downloads/libarchive-ruby/Rakefile.rb:77

I'm using Ruby 1.8.7 Enterprise Edition. When trying to install via Rubygems, it works but when I try to use it, this is what I get:

ree-1.8.7-2010.02 > require 'rubygems'
 => true 
ree-1.8.7-2010.02 > require 'archive'
 => true 
ree-1.8.7-2010.02 > a = Archive.new('/tmp/iAdTest.zip')
 => #<Archive:/tmp/iAdTest.zip> 
ree-1.8.7-2010.02 > a.extract
dyld: lazy symbol binding failed: Symbol not found: _archive_read_support_format_raw
  Referenced from: /Users/cristi/.rvm/gems/ree-1.8.7-2010.02/gems/libarchive-ruby-0.0.3/lib/archive.bundle
  Expected in: flat namespace

dyld: Symbol not found: _archive_read_support_format_raw
  Referenced from: /Users/cristi/.rvm/gems/ree-1.8.7-2010.02/gems/libarchive-ruby-0.0.3/lib/archive.bundle
  Expected in: flat namespace

[1]    25666 trace trap  irb

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.