473,226 Members | 1,567 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 software developers and data experts.

Zipping and unzipping

I was wondering if there was a way that I could take a file and compress or take a file and unzip it. What would the code for that be like?
Aug 27 '07 #1
6 1351
ilikepython
844 Expert 512MB
I was wondering if there was a way that I could take a file and compress or take a file and unzip it. What would the code for that be like?
I think there are a lot of modules to do this, like tarfile, zipfile, bz2, and some other.
Aug 27 '07 #2
I think there are a lot of modules to do this, like tarfile, zipfile, bz2, and some other.
Yes, but how? I looked up zipfile but didn't understand it.
Aug 27 '07 #3
ilikepython
844 Expert 512MB
Yes, but how? I looked up zipfile but didn't understand it.
Use a ZipFile object, particulary this page. Use the read and write, to unzip, and compress.
Aug 27 '07 #4
Bierny
12
Hi,
Some time ago i wrote a script that downloads a zip archive and unpacks it. Here is a part of code that was taking care of extracting all files from the archive.

Expand|Select|Wrap|Line Numbers
  1. # open an archive file
  2. my_zipfile = zipfile.ZipFile(sampleZIPFilename,'r')
  3.  
  4. # unzip all files from an archive
  5. for my_zippedfile in my_zipfile.namelist():
  6.     f = open(my_zippedfile,'w')
  7.     f.write(my_zipfile.read(my_zippedfile))
  8.     f.close()
  9.  
I hope this will be helpful ...

Best regards,
Aug 27 '07 #5
bvdet
2,851 Expert Mod 2GB
Yes, but how? I looked up zipfile but didn't understand it.
Following is a simple application of zipfile.Zipfile:
Expand|Select|Wrap|Line Numbers
  1. import zipfile, os
  2.  
  3. def makeArchive(fileList, archive):
  4.     try:
  5.         # ZipFile will accept a file name or file object
  6.         a = zipfile.ZipFile(archive, 'w', zipfile.ZIP_DEFLATED)
  7.         for f in fileList:
  8.             print "archiving file %s" % (f)
  9.             a.write(f) # (f, os.path.basename(f))
  10.         a.close()
  11.         return True
  12.     except: return False
  13.  
  14. if __name__== '__main__':
  15.  
  16.     fileList = ['H:/TEMP/temsys/abc1.txt',
  17.                 'H:/TEMP/temsys/abc2.txt',
  18.                 'H:/TEMP/temsys/abc2.txt',
  19.                 'H:/TEMP/temsys/abc3.txt',
  20.                 'H:/TEMP/temsys/abc4.txt'
  21.                 ]
  22.  
  23.     arcfile_name = 'H:/TEMP/temsys/zipped_files.zip'
  24.  
  25.     if makeArchive(fileList, arcfile_name):
  26.         print arcfile_name, "was created.\n"
  27.         # check the new archive file
  28.         f = zipfile.ZipFile(arcfile_name, 'r')
  29.         for info in f.infolist():
  30.             print info.filename, info.date_time, info.file_size, info.compress_size
  31.         f.close()
  32.     else:
  33.         print "There was an error"
Aug 27 '07 #6
Following is a simple application of zipfile.Zipfile:
Expand|Select|Wrap|Line Numbers
  1. import zipfile, os
  2.  
  3. def makeArchive(fileList, archive):
  4.     try:
  5.         # ZipFile will accept a file name or file object
  6.         a = zipfile.ZipFile(archive, 'w', zipfile.ZIP_DEFLATED)
  7.         for f in fileList:
  8.             print "archiving file %s" % (f)
  9.             a.write(f) # (f, os.path.basename(f))
  10.         a.close()
  11.         return True
  12.     except: return False
  13.  
  14. if __name__== '__main__':
  15.  
  16.     fileList = ['H:/TEMP/temsys/abc1.txt',
  17.                 'H:/TEMP/temsys/abc2.txt',
  18.                 'H:/TEMP/temsys/abc2.txt',
  19.                 'H:/TEMP/temsys/abc3.txt',
  20.                 'H:/TEMP/temsys/abc4.txt'
  21.                 ]
  22.  
  23.     arcfile_name = 'H:/TEMP/temsys/zipped_files.zip'
  24.  
  25.     if makeArchive(fileList, arcfile_name):
  26.         print arcfile_name, "was created.\n"
  27.         # check the new archive file
  28.         f = zipfile.ZipFile(arcfile_name, 'r')
  29.         for info in f.infolist():
  30.             print info.filename, info.date_time, info.file_size, info.compress_size
  31.         f.close()
  32.     else:
  33.         print "There was an error"
Okay, thanks for the help.
Aug 28 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Arthur | last post by:
Does anyone know if the new Windows 2003 built-in .zip compressor/decompressor is available to scripting? If so, can you kindly point me in the right direction? I've searched and searched MSDN...
7
by: thewhoracle | last post by:
I have a C# application that essentially builds batch and text files and runs them from the command line, but i need to unzip something from the command line. What i need is either an unzipping...
0
by: Benjamin Bittner | last post by:
hallo ng, first of all, for the zipping progress i use the ziplib from http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx now to my problem. im trying to build a little application...
9
by: ReidarT | last post by:
How can I zip (compress) a file in vs2005 with windows own zip-program? reidarT
0
by: stjulian | last post by:
On Windows Server 2003, IIS 6 I am looking for an unzipping component. I have a series of files that are available zipped at a considerable decrease in size. And before ASPINET fails on a script...
1
by: Brian | last post by:
Hi all Below is a bit of code that opens up and reads a zip file. I'm having trouble working out how to read in line by line. The file I am unzipping is a CSV file and I need to return each...
1
by: durumdara | last post by:
Hi! As I experienced in the year 2006, the Python's zip module is not unicode-safe. With the hungarian filenames I got wrong result. I need to convert iso-8859-2 to cp852 chset to get good...
0
by: avik1612 | last post by:
Hi, I have created a program to unzip the file. It is unzipping the zip files when put in directory i.e folder inside folder but when i create a zip file without putting in a folder and try to...
1
by: kss | last post by:
Hey all, I am new to python and I need to unzip a zip file which is password protected. I tried using zipfile class. Aparently, when i try doing a zip.read(filename), it throws an exception stating...
1
by: deepakd | last post by:
Hi I'm using wxZipInputStream for unzipping a zipped ".app" file in MAC OS.After unzipping i am not able to run the app.When i manually copy the unix executable to app packages, I am able to run...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.