<!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: &#24494;&#36719;&#38597;&#40657;; 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>&nbsp;</DIV>
<DIV>my code is:</DIV>
<DIV>&nbsp;</DIV>
<DIV>
<DIV>&nbsp;</DIV>
<DIV>import&nbsp;zipfile;</DIV>
<DIV>import&nbsp;tarfile;</DIV>
<DIV>import&nbsp;os;</DIV>
<DIV>from&nbsp;os&nbsp;import&nbsp;path&nbsp;;</DIV>
<DIV>&nbsp;</DIV>
<DIV>def&nbsp;showAllFiles(fileObj):</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;fileObj.filename.endswith("zip"):</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;isinstance(fileObj,&nbsp;zipfile.ZipFile):</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;"j"*20;</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;name&nbsp;in&nbsp;fileObj.namelist():</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;name;</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;else:</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;name&nbsp;in&nbsp;fileObj.getnames():</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;name;&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV>def&nbsp;moveFile(srcObj,&nbsp;dstObj):</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;fileName&nbsp;=&nbsp;raw_input("input&nbsp;the&nbsp;name&nbsp;of&nbsp;the&nbsp;file&nbsp;to&nbsp;move:&nbsp;");&nbsp;&nbsp;&nbsp;&nbsp;</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;srcObj.extract(fileName);</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;isinstance(dstObj,&nbsp;zipfile.ZipFile):</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dstObj.write(fileName);</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;else:</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dstObj.addfile(tarfile.TarInfo(fileName));</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;os.remove(fileName);&nbsp;&nbsp;&nbsp;&nbsp;</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;</DIV>
<DIV>def&nbsp;main():</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;intro&nbsp;=&nbsp;"""</DIV>
<DIV>enter&nbsp;a&nbsp;choice</DIV>
<DIV>(M)ove&nbsp;file&nbsp;from&nbsp;source&nbsp;file&nbsp;to&nbsp;destinatiom&nbsp;file</DIV>
<DIV>(S)how&nbsp;all&nbsp;the&nbsp;files&nbsp;in&nbsp;source&nbsp;file</DIV>
<DIV>(Q)uit</DIV>
<DIV>your&nbsp;choice&nbsp;is:&nbsp;"""&nbsp;&nbsp;&nbsp;&nbsp;</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;srcFile&nbsp;=&nbsp;raw_input("input&nbsp;the&nbsp;source&nbsp;file&nbsp;name:&nbsp;");</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;dstFile&nbsp;=&nbsp;raw_input("input&nbsp;the&nbsp;destination&nbsp;file&nbsp;name:&nbsp;");</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;True:</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with&nbsp;(&nbsp;zipfile.ZipFile(srcFile,&nbsp;"r")&nbsp;if&nbsp;srcFile.endswith("zip")&nbsp;else&nbsp;tarfile.open(srcFile,&nbsp;"r"+":"+path.splitext(srcFile)[1][1:])&nbsp;)&nbsp;as&nbsp;srcObj,&nbsp;\</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(&nbsp;zipfile.ZipFile(dstFile,&nbsp;"r")&nbsp;if</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dstFile.endswith("zip")&nbsp;else</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tarfile.open(dstFile,&nbsp;"w"+":"+path.splitext(dstFile)[1][1:])&nbsp;)&nbsp;as&nbsp;dstObj:&nbsp;&nbsp;&nbsp;&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p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;"invalid&nbsp;command!"</DIV>
<DIV>&nbsp;</DIV>
<DIV>if&nbsp;__name__&nbsp;==&nbsp;'__main__':</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;main();</DIV></DIV>
<DIV>&nbsp;</DIV>
<DIV>But there are some problems.</DIV>
<DIV>1. It could extract file&nbsp;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>&nbsp;</DIV>
<DIV>Please&nbsp; give me some help , thank you!</DIV>
<DIV>&nbsp;</DIV>
<HR style="WIDTH: 210px; HEIGHT: 1px" align=left color=#b5c4df SIZE=1>

<DIV><SPAN>daedae11</SPAN></DIV></BODY></HTML>