I'm trying to unzip a zip file in Django using the zipfile library.
This is my code:
if formtoaddmodel.is_valid():
content = request.FILES['content']
unzipped = zipfile.ZipFile(content)
print unzipped.namelist()
for libitem in unzipped.namelist():
filecontent = file(libitem,'wb').write(unzipped.read(libitem))
This is the output of print unzipped.namelist()
['FileName1.jpg', 'FileName2.png', '__MACOSX/', '__MACOSX/._FileName2.png']
Im wondering what the last two items are -- it looks like the path. I don't care about there -- so how is there a way to filter them out?
https://superuser.com/questions/104500/what-is-macosx-folder
if libitem.startswith('__MACOSX/'):
continue
Those files are tags added by the zip utility on MACS. You can assume the name starts with '__MACOSX/'
link
Related
I need help to read xlsx file present inside rar pack. I am using below code, however get an error. Is there any better way to read/extract file?
rar = glob.glob(INPATH + "*xyz*.rar*")
rf = rarfile.RarFile(rar[0])
for f in rf.infolist():
print(f.filename, f.file_size)
df = pd.read_excel(rf.read(f))
rarfile.RarCannotExec: Cannot find working tool
According to the brief PyPI docs, you need unrar installed and on your PATH in order for the module to work. It does not implement the RAR unpacking algorithm itself.
(Presumably you need rar as well, for creating archives.)
My zip file contains a lot of smaller zip files.
I want to iterate through all those files,
reading and printing each of their comments.
I've found out that zipfile file.zip or unzip -z file.zipcan do this to a file in separate, but I'm looking for a way to go through all of them.
Couldn't find anything perfect yet, but this post. However, the code is too advanced for me, and I need something very basic, to begin with :)
Any ideas or information would be great, thanks!
Not sure exactly what your looking for but here are a few ways I did it on an Ubuntu Linux machine.
for i in `ls *.zip`; do unzip -l $i; done
or
unzip -l myzip.zip
or
unzip -p myzip.zip | python -c 'import zipfile,sys,StringIO;print "\n".join(zipfile.ZipFile(StringIO.StringIO(sys.stdin.read())).namelist())'
You can use the zipfile library to iterate through your files and
get their comments using zipinfo.comment
import zipfile
file = zipfile.ZipFile('filepath.zip')
infolist = file.infolist()
for info in infolist:
print(info.comment)
The example above prints the comment of each file in your zip file.
You could loop through your zip files and print their contents comments similiarly.
Check out the official zipfile documentation, its super clear.
A short and easy way to achieve this:
from zipfile import ZipFile
ziplist = ZipFile('parentzip.zip').namelist()
for childzip in ziplist:
zip_comment = ZipFile(childzip).comment
Reminder that if you want to do string based comparisons you should either encode your reference string as bytes, or convert the comment into a string. Ex:
from zipfile import ZipFile
paths = ['file1.zip', 'file2.zip', 'file3.zip']
bad_str = 'please ignore me'
new = []
for filename in paths:
zip_comment = zipfile.ZipFile(filename).comment
if not zip_comment == str.encode(bad_str):
new.append(filename)
paths = new
I have created some csv files in my code and I would like to zip them as one folder to be sent by e-mail. I already have the e-mail function but the problem is to zip.
I tried to use this: here I am not extracting or find the files in a directory. I am creating the program the csv files and making a list of it.
My list of files is like this:
lista_files = [12.csv,13.csv,14.csv]
It seems to be easy for developers but as a beginning it is hard. I would really appreciate if someone can help me.
I believe you're looking for the zipfile library. And given that you're looking at a list of filenames, I'd just iterate using a for loop. If you have directories listed as well, you could use os.walk.
import zipfile
lista_files = ["12.csv","13.csv","14.csv"]
with zipfile.ZipFile('out.zip', 'w') as zipMe:
for file in lista_files:
zipMe.write(file, compress_type=zipfile.ZIP_DEFLATED)
Does anyone know how to compare amount of files and size of the files in archiwum.rar and its extracted content in the folder?
The reason I want to do this, is that server I'am working on has been restarted couple of times during extraction and I am not sure, if all the files has been extracted correctly.
.rar files are more then 100GB's each and server is not that fast.
Any ideas?
ps. if the solution would be some code instead standalone program, my preference is Python.
Thanks
In Python you can use RarFile module. The usage is similar to build-in module ZipFile.
import rarfile
import os.path
extracted_dir_name = "samples/sample" # Directory with extracted files
file = rarfile.RarFile("samples/sample.rar", "r")
# list file information
for info in file.infolist():
print info.filename, info.date_time, info.file_size
# Compare with extracted file here
extracted_file = os.path.join(extracted_dir_name, info.filename)
if info.file_size != os.path.getsize(extracted_file):
print "Different size!"
i want to upload zip folder from file input in form the i want to extract the contents of this uploaded zip folder,and store the contents (files)of this zip in the blobstore in order to download them after putting these files in one folder,but the problem is that i can't deal with the zip folder directly(to read it), i tried as this:
form = cgi.FieldStorage()
file_upload = form['file']
zip1=file_upload.filename
zipstream=StringIO.StringIO(zip1.read())
But the problem still that i can't read the zip as previous,also i tried to read zip folder directly like this:
z1=zipfile.ZipFile(zip1,"r")
But there was an error in this way.Please can any one help me.Thanks in advance.
Based on your comment, it sounds like you need to take a closer look at the cgi module documentation, which includes the following:
If a field represents an uploaded file, accessing the value via the value attribute or the getvalue() method reads the entire file in memory as a string. This may not be what you want. You can test for an uploaded file by testing either the filename attribute or the file attribute. You can then read the data at leisure from the file attribute...
This suggests that you need to modify your code to look something like:
form = cgi.FieldStorage()
file_upload = form['file']
z1 = zipfile.ZipFile(file_upload.file, 'r')
There are additional examples in the documentation.
You don't have to extract files from the zip in order to make them available for download - see this post for an example of serving direct from a zip. You can adapt that code if you want to extract the files and store them individually in the blobstore.