<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content="text/html; charset=us-ascii" http-equiv=Content-Type>
<STYLE>
BLOCKQUOTE {
        MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; MARGIN-LEFT: 2em
}
OL {
        MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px
}
UL {
        MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px
}
P {
        MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px
}
BODY {
        LINE-HEIGHT: 1.5; FONT-FAMILY: 微软雅黑; COLOR: #000000; FONT-SIZE: 10.5pt
}
</STYLE>
<META name=GENERATOR content="MSHTML 9.00.8112.16440"></HEAD>
<BODY style="MARGIN: 10px">
<DIV>I was asked to write a program to move files between ZIP(.zip) and
TAR/GZIP(.tgz/.tar.gz) or TAR/BZIP2(.tbz/.tar.bz2) archive. </DIV>
<DIV> </DIV>
<DIV>my code is:</DIV>
<DIV> </DIV>
<DIV>
<DIV> </DIV>
<DIV>import zipfile;</DIV>
<DIV>import tarfile;</DIV>
<DIV>import os;</DIV>
<DIV>from os import path ;</DIV>
<DIV> </DIV>
<DIV>def showAllFiles(fileObj):</DIV>
<DIV> if fileObj.filename.endswith("zip"):</DIV>
<DIV> if isinstance(fileObj, zipfile.ZipFile):</DIV>
<DIV> print "j"*20;</DIV>
<DIV> for name in fileObj.namelist():</DIV>
<DIV> print name;</DIV>
<DIV> else:</DIV>
<DIV> for name in fileObj.getnames():</DIV>
<DIV> print name; </DIV>
<DIV> </DIV>
<DIV>def moveFile(srcObj, dstObj):</DIV>
<DIV> fileName = raw_input("input the name of the file to move: "); </DIV>
<DIV> srcObj.extract(fileName);</DIV>
<DIV> if isinstance(dstObj, zipfile.ZipFile):</DIV>
<DIV> dstObj.write(fileName);</DIV>
<DIV> else:</DIV>
<DIV> dstObj.addfile(tarfile.TarInfo(fileName));</DIV>
<DIV> os.remove(fileName); </DIV>
<DIV> </DIV>
<DIV>def main():</DIV>
<DIV> intro = """</DIV>
<DIV>enter a choice</DIV>
<DIV>(M)ove file from source file to destinatiom file</DIV>
<DIV>(S)how all the files in source file</DIV>
<DIV>(Q)uit</DIV>
<DIV>your choice is: """ </DIV>
<DIV> srcFile = raw_input("input the source file name: ");</DIV>
<DIV> dstFile = raw_input("input the destination file name: ");</DIV>
<DIV> while True:</DIV>
<DIV> with ( zipfile.ZipFile(srcFile, "r") if srcFile.endswith("zip") else tarfile.open(srcFile, "r"+":"+path.splitext(srcFile)[1][1:]) ) as srcObj, \</DIV>
<DIV> ( zipfile.ZipFile(dstFile, "r") if</DIV>
<DIV> dstFile.endswith("zip") else</DIV>
<DIV> tarfile.open(dstFile, "w"+":"+path.splitext(dstFile)[1][1:]) ) as dstObj: </DIV>
<DIV> choice = raw_input(intro)[0].lower();</DIV>
<DIV> if choice == "s":</DIV>
<DIV> showAllFiles(srcObj);</DIV>
<DIV> elif choice == "m":</DIV>
<DIV> moveFile(srcObj, dstObj);</DIV>
<DIV> elif choice == "q":</DIV>
<DIV> break;</DIV>
<DIV> else:</DIV>
<DIV> print "invalid command!"</DIV>
<DIV> </DIV>
<DIV>if __name__ == '__main__':</DIV>
<DIV> main();</DIV></DIV>
<DIV> </DIV>
<DIV>But there are some problems.</DIV>
<DIV>1. It could extract file successfully, but can't add files to .tar.gz
file.</DIV>
<DIV>2. I think it's a little tedious, but I don't know how to improve it.</DIV>
<DIV> </DIV>
<DIV>Please give me some help , thank you!</DIV>
<DIV> </DIV>
<HR style="WIDTH: 210px; HEIGHT: 1px" align=left color=#b5c4df SIZE=1>
<DIV><SPAN>daedae11</SPAN></DIV></BODY></HTML>