Zipping and unzipping

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • psychofish25
    New Member
    • Jul 2007
    • 60

    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?
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by psychofish25
    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.

    Comment

    • psychofish25
      New Member
      • Jul 2007
      • 60

      #3
      Originally posted by ilikepython
      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.

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #4
        Originally posted by psychofish25
        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.

        Comment

        • Bierny
          New Member
          • Feb 2007
          • 12

          #5
          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.

          Code:
          # open an archive file
          my_zipfile = zipfile.ZipFile(sampleZIPFilename,'r')
          
          # unzip all files from an archive
          for my_zippedfile in my_zipfile.namelist():
              f = open(my_zippedfile,'w')
              f.write(my_zipfile.read(my_zippedfile))
              f.close()
          I hope this will be helpful ...

          Best regards,

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Originally posted by psychofish25
            Yes, but how? I looked up zipfile but didn't understand it.
            Following is a simple application of zipfile.Zipfile :[code=Python]import zipfile, os

            def makeArchive(fil eList, archive):
            try:
            # ZipFile will accept a file name or file object
            a = zipfile.ZipFile (archive, 'w', zipfile.ZIP_DEF LATED)
            for f in fileList:
            print "archiving file %s" % (f)
            a.write(f) # (f, os.path.basenam e(f))
            a.close()
            return True
            except: return False

            if __name__== '__main__':

            fileList = ['H:/TEMP/temsys/abc1.txt',
            'H:/TEMP/temsys/abc2.txt',
            'H:/TEMP/temsys/abc2.txt',
            'H:/TEMP/temsys/abc3.txt',
            'H:/TEMP/temsys/abc4.txt'
            ]

            arcfile_name = 'H:/TEMP/temsys/zipped_files.zi p'

            if makeArchive(fil eList, arcfile_name):
            print arcfile_name, "was created.\n"
            # check the new archive file
            f = zipfile.ZipFile (arcfile_name, 'r')
            for info in f.infolist():
            print info.filename, info.date_time, info.file_size, info.compress_s ize
            f.close()
            else:
            print "There was an error"[/code]

            Comment

            • psychofish25
              New Member
              • Jul 2007
              • 60

              #7
              Originally posted by bvdet
              Following is a simple application of zipfile.Zipfile :[code=Python]import zipfile, os

              def makeArchive(fil eList, archive):
              try:
              # ZipFile will accept a file name or file object
              a = zipfile.ZipFile (archive, 'w', zipfile.ZIP_DEF LATED)
              for f in fileList:
              print "archiving file %s" % (f)
              a.write(f) # (f, os.path.basenam e(f))
              a.close()
              return True
              except: return False

              if __name__== '__main__':

              fileList = ['H:/TEMP/temsys/abc1.txt',
              'H:/TEMP/temsys/abc2.txt',
              'H:/TEMP/temsys/abc2.txt',
              'H:/TEMP/temsys/abc3.txt',
              'H:/TEMP/temsys/abc4.txt'
              ]

              arcfile_name = 'H:/TEMP/temsys/zipped_files.zi p'

              if makeArchive(fil eList, arcfile_name):
              print arcfile_name, "was created.\n"
              # check the new archive file
              f = zipfile.ZipFile (arcfile_name, 'r')
              for info in f.infolist():
              print info.filename, info.date_time, info.file_size, info.compress_s ize
              f.close()
              else:
              print "There was an error"[/code]
              Okay, thanks for the help.

              Comment

              Working...