Skip to content

Commit

Permalink
add zip file code
Browse files Browse the repository at this point in the history
  • Loading branch information
ming71 committed Dec 16, 2019
1 parent 0710a07 commit 3d9fd86
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions zip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import os,os.path
import zipfile

def zip_dir(dirname,zipfilename):
filelist = []
if os.path.isfile(dirname):
filelist.append(dirname)
else :
for root, dirs, files in os.walk(dirname):
for name in files:
filelist.append(os.path.join(root, name))

zf = zipfile.ZipFile(zipfilename, "w", zipfile.zlib.DEFLATED)
for tar in filelist:
arcname = tar[len(dirname):]
#print arcname
zf.write(tar,arcname)
zf.close()


def unzip_file(zipfilename, unziptodir):
if not os.path.exists(unziptodir):
os.mkdir(unziptodir)
zfobj = zipfile.ZipFile(zipfilename)
for name in zfobj.namelist():
name = name.replace('\\','/')

if name.endswith('/'):
os.mkdir(os.path.join(unziptodir, name))
else:
ext_filename = os.path.join(unziptodir, name)
ext_dir= os.path.dirname(ext_filename)
if not os.path.exists(ext_dir) :
os.mkdir(ext_dir)
outfile = open(ext_filename, 'wb')
outfile.write(zfobj.read(name))
outfile.close()

if __name__ == '__main__':

## zip files
# dirname = 'ship'
# zipfilename = 'ship.zip'
# zip_dir(dirname, zipfilename)

## unzip files
# zipfilename = 'ship.zip'
# unziptodir = 'ship'
# unzip_file(zipfilename, unziptodir)

0 comments on commit 3d9fd86

Please sign in to comment.