471,353 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,353 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 1198
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

Post your reply

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

Similar topics

7 posts views Thread by thewhoracle | last post: by
reply views Thread by Benjamin Bittner | last post: by
reply views Thread by stjulian | last post: by
1 post views Thread by Brian | last post: by
1 post views Thread by durumdara | last post: by
1 post views Thread by kss | last post: by
1 post views Thread by deepakd | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.