From: Kevin J. B. <jyt...@sa...> - 2002-12-10 21:49:04
|
> > >From: kco...@cf... > >HELP>>>>I concede, I really don’t know what else to do. My dilemma…… I need to >unzip from jython. The zip file is quite large with a directory structure. >(the zip file is apache 2.0.39) My jython unzip.py is not handling the write >or read well; I’m doing something wrong with the input and output stream, I >gave up. > The following works for me: from java.util.zip import ZipFile from java.io import * import jarray BUFSIZE=8192 def copy( input, output, bufsize=BUFSIZE ): buffer = jarray.zeros( bufsize, 'b' ) total = 0 while 1: bytesRead = input.read( buffer, 0, bufsize ) if bytesRead == -1: break else: output.write( buffer, 0, bytesRead ) total = total + bytesRead return total def extract( filename, dest=None ): zipfile = ZipFile( File( filename ), ZipFile.OPEN_READ ) try: enum = zipfile.entries() while enum.hasMoreElements(): entry = enum.nextElement() name = entry.getName() print name, file = File( dest, name ) if entry.isDirectory(): print "directory" file.mkdirs() else: input = zipfile.getInputStream( entry ) output = BufferedOutputStream( FileOutputStream( file )) try: size = copy( input, output ) print size, "bytes" finally: output.close() finally: zipfile.close() if __name__ == "__main__": import sys extract( sys.argv[1] ) >So I created a python module that works wonderfully. Pretty much the same code >as above. Because I am deploying to users and do not want to install python I >created a standalone unzip.exe. Run this by itself, it works great. My >problem is when I execute from jython the process does not finish, it just >hangs about half way through reading and write the files over the targetDir.. >I’m not handling the 'process' correctly but I don’t know what to do. > The problem is most likely that you're not reading the process's output as it runs, so its stdout buffer gets full, causing it to block. You're blocked waiting for the process to complete => deadlock. The os.system and os.popen* commands in Jython CVS handle this appropriately. kb |