2011-10-18 12 views
30

मुझे 2.4 के साथ एक ज़िप फ़ाइल को अनजिप करने का तरीका पता लगाना मुश्किल समय है। extract() 2.4 में शामिल नहीं है। मैं अपने सर्वर पर 2.4.4 का उपयोग करने के लिए प्रतिबंधित हूँ।पायथन 2.4 के साथ फ़ाइल को अनजिप कैसे करें?

क्या कोई कृपया एक साधारण कोड उदाहरण प्रदान कर सकता है?

+0

आप प्रणाली समारोह निष्पादित "' '" बैकटिक ऑपरेटर या किसी अन्य तरीके का उपयोग करें और अपने फ़ाइल अनज़िप – user973254

+1

क्या आप के साथ क्या करना चाहते हो सकता है प्रकट हो सकते हैं लेना चाहिए एक बैकटिक? ऊ – naeg

+3

आप इस सवाल का पाया, लेकिन अजगर के एक नए संस्करण का उपयोग कर रहे हैं, तो ऐसा करते हैं: zfile = zipfile.ZipFile (file_to_extract) zfile.extractall (target_dir) – Fabian

उत्तर

51

आपको namelist() और extract() का उपयोग करना होगा। नमूना पर विचार निर्देशिका

import zipfile 
import os.path 
import os 
zfile = zipfile.ZipFile("test.zip") 
for name in zfile.namelist(): 
    (dirname, filename) = os.path.split(name) 
    print "Decompressing " + filename + " on " + dirname 
    if not os.path.exists(dirname): 
    os.makedirs(dirname) 
    zfile.extract(name, dirname) 
+0

यह काम करता है। बहुत - बहुत धन्यवाद। – Tapefreak

+2

इस की प्रतिलिपि बनाकर एक छोटी सी बात देखी। कम से कम यदि आप अपनी फ़ाइल को Win7 "sendTo ज़िप फ़ाइल" विकल्प के साथ ज़िप करते हैं, और आपकी ज़िप फ़ाइल में नेस्टेड फ़ोल्डर्स हैं, तो आपको os.mkdir (dirname) -> os.makedirs (dirname) को बदलने की आवश्यकता है।अन्यथा आपको अपवाद (ऐसी कोई फ़ाइल या निर्देशिका नहीं) मिल सकती है, क्योंकि ज़िप फ़ाइल में केवल पत्ते के फ़ोल्डर्स – fastfox

+1

होते हैं यदि 'name' एक निर्देशिका है (नियमित फ़ाइल नहीं) तो क्या होगा? मैं इस मामले में आया था। – Nawaz

1

पूरी तरह से परीक्षण नहीं है, लेकिन यह ठीक किया जाना चाहिए:

import os 
from zipfile import ZipFile, ZipInfo 

class ZipCompat(ZipFile): 
    def __init__(self, *args, **kwargs): 
     ZipFile.__init__(self, *args, **kwargs) 

    def extract(self, member, path=None, pwd=None): 
     if not isinstance(member, ZipInfo): 
      member = self.getinfo(member) 
     if path is None: 
      path = os.getcwd() 
     return self._extract_member(member, path) 

    def extractall(self, path=None, members=None, pwd=None): 
     if members is None: 
      members = self.namelist() 
     for zipinfo in members: 
      self.extract(zipinfo, path) 

    def _extract_member(self, member, targetpath): 
     if (targetpath[-1:] in (os.path.sep, os.path.altsep) 
      and len(os.path.splitdrive(targetpath)[1]) > 1): 
      targetpath = targetpath[:-1] 
     if member.filename[0] == '/': 
      targetpath = os.path.join(targetpath, member.filename[1:]) 
     else: 
      targetpath = os.path.join(targetpath, member.filename) 
     targetpath = os.path.normpath(targetpath) 
     upperdirs = os.path.dirname(targetpath) 
     if upperdirs and not os.path.exists(upperdirs): 
      os.makedirs(upperdirs) 
     if member.filename[-1] == '/': 
      if not os.path.isdir(targetpath): 
       os.mkdir(targetpath) 
      return targetpath 
     target = file(targetpath, "wb") 
     try: 
      target.write(self.read(member.filename)) 
     finally: 
      target.close() 
     return targetpath 
12

वहाँ Vinko के जवाब के साथ कुछ समस्या है (कम से कम जब मैं इसे चलाने)। मुझे मिल गया:

IOError: [Errno 13] Permission denied: '01org-webapps-countingbeads-422c4e1/' 

यहाँ यह कैसे हल करने के लिए है:

# unzip a file 
def unzip(path): 
    zfile = zipfile.ZipFile(path) 
    for name in zfile.namelist(): 
     (dirname, filename) = os.path.split(name) 
     if filename == '': 
      # directory 
      if not os.path.exists(dirname): 
       os.mkdir(dirname) 
     else: 
      # file 
      fd = open(name, 'w') 
      fd.write(zfile.read(name)) 
      fd.close() 
    zfile.close() 
+2

यह मेरे लिए भी काम करता है। धन्यवाद। –

+1

क्या कुछ ज़िप्ड फाइलें छवियां या अन्यथा द्विआधारी फाइलें हैं, तो यह 'fd = open (name,' wb ') नहीं होना चाहिए? – ArtB

3

Ovilia's answer संशोधित करना इतना है कि आप गंतव्य निर्देशिका के रूप में अच्छी तरह से निर्दिष्ट कर सकते हैं:

def unzip(zipFilePath, destDir): 
    zfile = zipfile.ZipFile(zipFilePath) 
    for name in zfile.namelist(): 
     (dirName, fileName) = os.path.split(name) 
     if fileName == '': 
      # directory 
      newDir = destDir + '/' + dirName 
      if not os.path.exists(newDir): 
       os.mkdir(newDir) 
     else: 
      # file 
      fd = open(destDir + '/' + name, 'wb') 
      fd.write(zfile.read(name)) 
      fd.close() 
    zfile.close() 
-1

मैं अजगर 2.7 में परीक्षण कर रहा हूँ। 3rc2 और ZipFile.namelist() उप निर्देशिका बनाने के लिए केवल उप निर्देशिका नाम के साथ एक प्रविष्टि नहीं लौटा रहा है, लेकिन उप निर्देशिका के साथ फ़ाइल नामों की केवल एक सूची निम्नानुसार है:

['20130923104558/control.json', '20130923104558/test.csv']

इस प्रकार की जांच

if fileName == '':

बिल्कुल True का मूल्यांकन नहीं करता है।

इसलिए मैंने dirNamedestDir के अंदर मौजूद है और dirName बनाने के लिए कोड मौजूद है या नहीं, तो यह कोड संशोधित किया गया है। फ़ाइल केवल तभी निकाली जाती है जब fileName हिस्सा खाली नहीं है। तो इस हालत का ख्याल जहां एक निर्देशिका नाम पर ZipFile.namelist()

def unzip(zipFilePath, destDir): 
    zfile = zipfile.ZipFile(zipFilePath) 
    for name in zfile.namelist(): 
     (dirName, fileName) = os.path.split(name) 
     # Check if the directory exisits 
     newDir = destDir + '/' + dirName 
     if not os.path.exists(newDir): 
      os.mkdir(newDir) 
     if not fileName == '': 
      # file 
      fd = open(destDir + '/' + name, 'wb') 
      fd.write(zfile.read(name)) 
      fd.close() 
    zfile.close() 
+0

यदि आप अजगर चला रहे हैं तो यह जवाब नहीं है 2.7 प्रश्न पर मेरी टिप्पणी देखें – Fabian