unzip (zip, tar, tag.gz) files with ruby

To extract files from a .tar.gz file you can use the following methods from packages distributed with Ruby: require ‘rubygems/package’ require ‘zlib’ tar_extract = Gem::Package::TarReader.new(Zlib::GzipReader.open(‘Path/To/myfile.tar.gz’)) tar_extract.rewind # The extract has to be rewinded after every iteration tar_extract.each do |entry| puts entry.full_name puts entry.directory? puts entry.file? # puts entry.read end tar_extract.close Each entry of type Gem::Package::TarReader::Entry … Read more

tar: file changed as we read it

I also encounter the tar messages “changed as we read it”. For me these message occurred when I was making tar file of Linux file system in bitbake build environment. This error was sporadic. For me this was not due to creating tar file from the same directory. I am assuming there is actually some … Read more

How to Compress/Decompress tar.gz files in java

I’ve written a wrapper for commons-compress called jarchivelib that makes it easy to extract or compress from and into File objects. Example code would look like this: File archive = new File(“/home/thrau/archive.tar.gz”); File destination = new File(“/home/thrau/archive/”); Archiver archiver = ArchiverFactory.createArchiver(“tar”, “gz”); archiver.extract(archive, destination);

How can I build a tar from stdin?

Something like: tar cfz foo.tgz –files-from=- But keep in mind that this won’t work for all possible filenames; you should consider the –null option and feed tar from find -print0. (The xargs example won’t quite work for large file lists because it will spawn multiple tar commands.)

python write string directly to tarfile

I would say it’s possible, by playing with TarInfo e TarFile.addfile passing a StringIO as a fileobject. Very rough, but works import tarfile import StringIO tar = tarfile.TarFile(“test.tar”,”w”) string = StringIO.StringIO() string.write(“hello”) string.seek(0) info = tarfile.TarInfo(name=”foo”) info.size=len(string.buf) tar.addfile(tarinfo=info, fileobj=string) tar.close()

Find files and tar them (with spaces)

Use this: find . -type f -print0 | tar -czvf backup.tar.gz –null -T – It will: deal with files with spaces, newlines, leading dashes, and other funniness handle an unlimited number of files won’t repeatedly overwrite your backup.tar.gz like using tar -c with xargs will do when you have a large number of files Also … Read more