Questions tagged [python-zipfile]

Python standard-library module provides tools to create, read, write, append, and list a ZIP file.

404 questions
849
votes
29 answers

How to create a zip archive of a directory?

How can I create a zip archive of a directory structure in Python?
Martha Yi
  • 8,633
  • 3
  • 18
  • 8
835
votes
9 answers

Unzipping files in Python

I read through the zipfile documentation, but couldn't understand how to unzip a file, only how to zip a file. How do I unzip all the contents of a zip file into the same directory?
John Howard
  • 61,037
  • 23
  • 50
  • 66
103
votes
3 answers

get file list of files contained in a zip file

I have a zip archive: my_zip.zip. Inside it is one txt file, the name of which I do not know. I was taking a look at Python's zipfile module ( http://docs.python.org/library/zipfile.html ), but couldn't make too much sense of what I'm trying to…
David542
  • 104,438
  • 178
  • 489
  • 842
103
votes
6 answers

How to eliminate absolute path in zip archive if absolute paths for files are provided?

I have two files in two different directories, one is '/home/test/first/first.pdf', the other is '/home/text/second/second.pdf'. I use following code to compress them: import zipfile, StringIO buffer = StringIO.StringIO() first_path =…
Shang Wang
  • 24,909
  • 20
  • 73
  • 94
95
votes
3 answers

python zipfile module doesn't seem to be compressing my files

I made a little helper function: import zipfile def main(archive_list=[],zfilename='default.zip'): print zfilename zout = zipfile.ZipFile(zfilename, "w") for fname in archive_list: print "writing: ", fname …
Ramy
  • 20,541
  • 41
  • 103
  • 153
87
votes
3 answers

Extracting a zipfile to memory?

How do I extract a zip to memory? My attempt (returning None on .getvalue()): from zipfile import ZipFile from StringIO import StringIO def extract_zip(input_zip): return StringIO(ZipFile(input_zip).extractall())
user1438003
  • 6,603
  • 8
  • 30
  • 36
72
votes
5 answers

Extract files from zip without keeping the structure using python ZipFile?

I try to extract all files from .zip containing subfolders in one folder. I want all the files from subfolders extract in only one folder without keeping the original structure. At the moment, I extract all, move the files to a folder, then remove…
Thammas
  • 973
  • 2
  • 9
  • 14
49
votes
8 answers

How do I set permissions (attributes) on a file in a ZIP file using Python's zipfile module?

When I extract files from a ZIP file created with the Python zipfile module, all the files are not writable, read only etc. The file is being created and extracted under Linux and Python 2.5.2. As best I can tell, I need to set the…
Tom
  • 42,844
  • 35
  • 95
  • 101
44
votes
8 answers

How to create an encrypted ZIP file?

I am creating an ZIP file with ZipFile in Python 2.5, it works OK so far: import zipfile, os locfile = "test.txt" loczip = os.path.splitext (locfile)[0] + ".zip" zip = zipfile.ZipFile (loczip, "w") zip.write (locfile) zip.close() But I couldn't…
PabloG
  • 25,761
  • 10
  • 46
  • 59
37
votes
3 answers

Create .zip in Python?

I'm trying to create a function in my script that zips the contents of a given source directory (src) to a zip file (dst). For example, zip('/path/to/dir', '/path/to/file.zip'), where /path/to/dir is a directory, and /path/to/file.zip doesn't exist…
tkbx
  • 15,602
  • 32
  • 87
  • 122
30
votes
4 answers

Safely extract zip or tar using Python

I'm trying to extract user-submitted zip and tar files to a directory. The documentation for zipfile's extractall method (similarly with tarfile's extractall) states that it's possible for paths to be absolute or contain .. paths that go outside the…
jterrace
  • 64,866
  • 22
  • 157
  • 202
25
votes
5 answers

renaming the extracted file from zipfile

I have lots of zipped files on a Linux server and each file includes multiple text files. what I want is to extract some of those text files, which have the same name across zipped files and save it a folder; I am creating one folder for each…
Roo
  • 862
  • 1
  • 10
  • 22
25
votes
2 answers

Adding file to existing zipfile

I'm using python's zipfile module. Having a zip file located in a path of: /home/user/a/b/c/test.zip And having another file created under /home/user/a/b/c/1.txt I want to add this file to existing zip, I did: zip =…
JavaSa
  • 5,813
  • 16
  • 71
  • 121
23
votes
1 answer

Unzip buffer with Python?

I have a buffer of bytes read from a library call and I would like to unzip the content which is a single text file. I tried with zlib, but I get this error: >>> import zlib >>> zlib.decompress(buffer) error: Error -3 while decompressing data:…
nowox
  • 25,978
  • 39
  • 143
  • 293
21
votes
1 answer

Faster alternative to Python's zipfile module?

Is there a noticeably faster alternative to Python 2.7.4 zipfile module (with ZIP_DEFLATED) for zipping a large number of files into a single zip file? I had a look at czipfile https://pypi.python.org/pypi/czipfile/1.0.0, but that appears to be…
michaelhubbard.ca
  • 253
  • 1
  • 2
  • 10
1
2 3
26 27