PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Getting files out of a .tar.gz archive
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Getting files out of a .tar.gz archive

Réponse
 
LinkBack Outils de la discussion
Vieux 10/03/2008, 15h09   #1
celldee
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Getting files out of a .tar.gz archive

Hi,

I have a .tar.gz file containing some xml files. I need to locate
particular files in the archive and process them. I'm looking for a
pure Ruby way to do this without resorting to external system
commands.

I found Archive::Tar::Minitar that allows me to process my files once
I have expanded the .tar.gz file to a .tar file. So I can do this :-

open(@tarfile, "rb") do |f|
Archive::Tar::Minitar::Reader.open(f).each do |entry|
fpl = StringIO.new( entry.read) if entry.name =~ /
#{@date}#{channel}_pl/
fpi = StringIO.new( entry.read) if entry.name =~ /
#{@date}#{channel}_pi/
end
end

However, in order to get a tar file, I have to call gunzip to expand
my .tar.gz file. Does anybody know of a way for me to replace the
gunzip call with a Ruby library call of some sort? Or does anyone have
suggestions for an alternative way to do this?

Cheers,

Chris
http://smuby.org

  Réponse avec citation
Vieux 10/03/2008, 16h19   #2
Todd Benson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Getting files out of a .tar.gz archive

On Mon, Mar 10, 2008 at 9:09 AM, celldee <celldee@gmail.com> wrote:
> Hi,
>
> I have a .tar.gz file containing some xml files. I need to locate
> particular files in the archive and process them. I'm looking for a
> pure Ruby way to do this without resorting to external system
> commands.
>
> I found Archive::Tar::Minitar that allows me to process my files once
> I have expanded the .tar.gz file to a .tar file. So I can do this :-
>
> open(@tarfile, "rb") do |f|
> Archive::Tar::Minitar::Reader.open(f).each do |entry|
> fpl = StringIO.new( entry.read) if entry.name =~ /
> #{@date}#{channel}_pl/
> fpi = StringIO.new( entry.read) if entry.name =~ /
> #{@date}#{channel}_pi/
> end
> end
>
> However, in order to get a tar file, I have to call gunzip to expand
> my .tar.gz file. Does anybody know of a way for me to replace the
> gunzip call with a Ruby library call of some sort? Or does anyone have
> suggestions for an alternative way to do this?
>
> Cheers,
>
> Chris


There's Zlib::Gzip* classes. I've never used them, though.

Todd

  Réponse avec citation
Vieux 10/03/2008, 17h46   #3
celldee
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Getting files out of a .tar.gz archive

Hi Todd,

I've tried to use Zlib::GzipReader, but that just gives me a
continuous stream of text or a series of strings that do not resemble
the actual file structure, unless I've missed something.

Thanks,

Chris
http://smuby.org

On Mar 10, 3:19=A0pm, "Todd Benson" <caduce...@gmail.com> wrote:
> On Mon, Mar 10, 2008 at 9:09 AM, celldee <cell...@gmail.com> wrote:
> > Hi,

>
> > =A0I have a .tar.gz file containing some xml files. I need to locate
> > =A0particular files in the archive and process them. I'm looking for a
> > =A0pure Ruby way to do this without resorting to external system
> > =A0commands.

>
> > =A0I found Archive::Tar::Minitar that allows me to process my files once=


> > =A0I have expanded the .tar.gz file to a .tar file. So I can do this :-

>
> > =A0open(@tarfile, "rb") do |f|
> > =A0 Archive::Tar::Minitar::Reader.open(f).each do |entry|
> > =A0 =A0 fpl =3D StringIO.new( entry.read) if entry.name =3D~ /
> > =A0#{@date}#{channel}_pl/
> > =A0 =A0 fpi =3D StringIO.new( entry.read) if entry.name =3D~ /
> > =A0#{@date}#{channel}_pi/
> > =A0 end
> > =A0end

>
> > =A0However, in order to get a tar file, I have to call gunzip to expand
> > =A0my .tar.gz file. Does anybody know of a way for me to replace the
> > =A0gunzip call with a Ruby library call of some sort? Or does anyone hav=

e
> > =A0suggestions for an alternative way to do this?

>
> > =A0Cheers,

>
> > =A0Chris

>
> There's Zlib::Gzip* classes. =A0I've never used them, though.
>
> Todd


  Réponse avec citation
Vieux 10/03/2008, 18h04   #4
Daniel Brumbaugh Keeney
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Getting files out of a .tar.gz archive

On Mon, Mar 10, 2008 at 9:09 AM, celldee <celldee@gmail.com> wrote:
> Hi,
>
> I have a .tar.gz file containing some xml files. I need to locate
> particular files in the archive and process them. I'm looking for a
> pure Ruby way to do this without resorting to external system
> commands.
>
> I found Archive::Tar::Minitar that allows me to process my files once
> I have expanded the .tar.gz file to a .tar file. So I can do this :-
>
> open(@tarfile, "rb") do |f|
> Archive::Tar::Minitar::Reader.open(f).each do |entry|
> fpl = StringIO.new( entry.read) if entry.name =~ /
> #{@date}#{channel}_pl/
> fpi = StringIO.new( entry.read) if entry.name =~ /
> #{@date}#{channel}_pi/
> end
> end
>
> However, in order to get a tar file, I have to call gunzip to expand
> my .tar.gz file. Does anybody know of a way for me to replace the
> gunzip call with a Ruby library call of some sort? Or does anyone have
> suggestions for an alternative way to do this?
>
> Cheers,
>
> Chris
> http://smuby.org
>
>


Use the docs.

From Minitar's readme:
tgz = Zlib::GzipReader.new(File.open('test.tgz', 'rb'))
# Warning: tgz and the file will be closed.
Minitar.unpack(tgz, 'x')

For GZip and the rest of the standard library,
http://www.ruby-doc.org/stdlib/

Daniel Brumbaugh Keeney

  Réponse avec citation
Vieux 10/03/2008, 18h43   #5
celldee
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Getting files out of a .tar.gz archive

Hi Daniel,

I saw that, but I don't want to expand the .tar.gz any more than I
have to. The code that I put up earlier is getting what I want out of
the .tar file using Minitar::Reader which I quite like, I'm just
looking to eliminate the gunzip step. Minitar.unpack expands
the .tar.gz and writes the files to disk, which means that I'll have
to mess around in the filesystem more than I need to.

Thanks for your reply,

Chris
http://smuby.org

On Mar 10, 5:04=A0pm, "Daniel Brumbaugh Keeney"
<devi.webmas...@gmail.com> wrote:
> On Mon, Mar 10, 2008 at 9:09 AM, celldee <cell...@gmail.com> wrote:
> > Hi,

>
> > =A0I have a .tar.gz file containing some xml files. I need to locate
> > =A0particular files in the archive and process them. I'm looking for a
> > =A0pure Ruby way to do this without resorting to external system
> > =A0commands.

>
> > =A0I found Archive::Tar::Minitar that allows me to process my files once=


> > =A0I have expanded the .tar.gz file to a .tar file. So I can do this :-

>
> > =A0open(@tarfile, "rb") do |f|
> > =A0 Archive::Tar::Minitar::Reader.open(f).each do |entry|
> > =A0 =A0 fpl =3D StringIO.new( entry.read) if entry.name =3D~ /
> > =A0#{@date}#{channel}_pl/
> > =A0 =A0 fpi =3D StringIO.new( entry.read) if entry.name =3D~ /
> > =A0#{@date}#{channel}_pi/
> > =A0 end
> > =A0end

>
> > =A0However, in order to get a tar file, I have to call gunzip to expand
> > =A0my .tar.gz file. Does anybody know of a way for me to replace the
> > =A0gunzip call with a Ruby library call of some sort? Or does anyone hav=

e
> > =A0suggestions for an alternative way to do this?

>
> > =A0Cheers,

>
> > =A0Chris
> > =A0http://smuby.org

>
> Use the docs.
>
> From Minitar's readme:
> tgz =3D Zlib::GzipReader.new(File.open('test.tgz', 'rb'))
> # Warning: tgz and the file will be closed.
> Minitar.unpack(tgz, 'x')
>
> For GZip and the rest of the standard library,http://www.ruby-doc.org/stdl=

ib/
>
> Daniel Brumbaugh Keeney


  Réponse avec citation
Vieux 10/03/2008, 18h55   #6
Joachim Glauche
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Getting files out of a .tar.gz archive

celldee wrote:
> I saw that, but I don't want to expand the .tar.gz any more than I
> have to. The code that I put up earlier is getting what I want out of
> the .tar file using Minitar::Reader which I quite like, I'm just
> looking to eliminate the gunzip step.


If packed with Gzip, you always have to unpack it in order to read the
content.

Tar is the container and Gzip does the compression. So the files in a
tar.gz file are first put into a container then packed. So in order you
read anything inside the tar you have to unpack the file to have access
the tarball to read your file inside.
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 10/03/2008, 19h52   #7
Paul Mckibbin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Getting files out of a .tar.gz archive

Chris,

Persevere with Zlib::GzipReader, that'll give you what you want.

Mac
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 11/03/2008, 01h01   #8
Daniel Brumbaugh Keeney
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Getting files out of a .tar.gz archive

On Mon, Mar 10, 2008 at 12:43 PM, celldee <celldee@gmail.com> wrote:
> Hi Daniel,
>
> I saw that, but I don't want to expand the .tar.gz any more than I
> have to. The code that I put up earlier is getting what I want out of
> the .tar file using Minitar::Reader which I quite like, I'm just
> looking to eliminate the gunzip step. Minitar.unpack expands
> the .tar.gz and writes the files to disk, which means that I'll have
> to mess around in the filesystem more than I need to.
>
> Thanks for your reply,
>
> Chris



My apologies for failing to understand the issue. GZip and Minitar
both provide incremental readers, although I have not used them. I
believe the correct combination for what you're asking is this:

tgz = Zlib::GzipReader.new(File.open('test.tgz', 'rb'))
# Warning: tgz and the file will be closed.
reader = Minitar::Reader.new(tgz)
reader.each_entry do |file|
#do something with each file, and break if you like
end
reader.close # does this do anything?
tgz.close

Daniel Brumbaugh Keeney

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 14h06.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,24921 seconds with 16 queries