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

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

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

How to check if a Unix .tar.gz file is a valid file without uncompressing?

What about just getting a listing of the tarball and throw away the output, rather than decompressing the file? tar -tzf my_tar.tar.gz >/dev/null Edited as per comment. Thanks zrajm! Edit as per comment. Thanks Frozen Flame! This test in no way implies integrity of the data. Because it was designed as a tape archival utility … Read more

How can I read a .tar.gz file with PHP?

You can do this with the PharData class: // Example: list files $archive = new PharData(‘/some/file.tar.gz’); foreach($archive as $file) { echo “$file\n”; } This even works with the phar:// stream wrapper: $list = scandir(‘phar:///some/file.tar.gz’); $fd = fopen(‘phar:///some/file.tar.gz/some/file/in/the/archive’, ‘r’); $contents = file_get_contents(‘phar:///some/file.tar.gz/some/file/in/the/archive’); If you don’t have Phar, check the PHP-only implementation, or the pecl extension.

reading tar file contents without untarring it, in python script

you can use getmembers() >>> import tarfile >>> tar = tarfile.open(“test.tar”) >>> tar.getmembers() After that, you can use extractfile() to extract the members as file object. Just an example import tarfile,os import sys os.chdir(“/tmp/foo”) tar = tarfile.open(“test.tar”) for member in tar.getmembers(): f=tar.extractfile(member) content=f.read() print “%s has %d newlines” %(member, content.count(“\n”)) print “%s has %d spaces” … Read more

How do I extract a tar file in Java?

You can do this with the Apache Commons Compress library. You can download the 1.2 version from http://mvnrepository.com/artifact/org.apache.commons/commons-compress/1.2. Here are two methods: one that unzips a file and another one that untars it. So, for a file <fileName>tar.gz, you need to first unzip it and after that untar it. Please note that the tar archive … Read more