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

Read .tar.gz file in Python

The docs tell us that None is returned by extractfile() if the member is a not a regular file or link. One possible solution is to skip over the None results: tar = tarfile.open(“filename.tar.gz”, “r:gz”) for member in tar.getmembers(): f = tar.extractfile(member) if f is not None: content = f.read()

tar with –include pattern

GNU tar has a -T or –files-from option that can take a file containing a list of files to include. The file specified with this option can be “-” for stdin. So, you can pass an arbitrary list of files for tar to archive from stdin using -files-from –. Using find patterns to generate a … Read more

how to make tar exclude hidden directories

You can use –exclude=”.*” $ tar -czvf test.tgz test/ test/ test/seen test/.hidden $ tar –exclude=”.*” -czvf test.tgz test/ test/ test/seen Be careful if you are taring the current directory, since it will also be excluded by this pattern matching. $ cd test $ tar –exclude=”.*” -czvf test.tgz ./ $ tar -czvf test.tgz ./ ./ ./seen … Read more

How do I exclude absolute paths for tar?

If you want to remove the first n leading components of the file name, you need strip-components. So in your case, on extraction, do tar xvf tarname.tar –strip-components=2 The man page has a list of tar‘s many options, including this one. Some earlier versions of tar use –strip-path for this operation instead.

Programmatically extract tar.gz in a single step (on Windows with 7-Zip)

Old question, but I was struggling with it today so here’s my 2c. The 7zip commandline tool “7z.exe” (I have v9.22 installed) can write to stdout and read from stdin so you can do without the intermediate tar file by using a pipe: 7z x “somename.tar.gz” -so | 7z x -aoa -si -ttar -o”somename” Where: … Read more

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()

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)