1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
from java.util.zip import ZipEntry, ZipOutputStream
from java.io import File, FileInputStream, ByteArrayOutputStream, FileOutputStream, ByteArrayInputStream
import string, os, jarray, sys
class DirectoryOutput:
def __init__(self, dirname):
self.outdir = dirname
def getFile(self, name):
fname = apply(os.path.join, tuple([self.outdir]+string.split(name, '.')))+'.class'
file = File(fname)
File(file.getParent()).mkdirs()
return FileOutputStream(file)
def write(self, name, file):
fp = self.getFile(name)
if isinstance(file, ByteArrayOutputStream):
file.writeTo(fp)
else:
if isinstance(file, type('')):
file = FileInputStream(file)
data = jarray.zeros(1024*4, 'b')
#print 'writing', file,
while 1:
n = file.read(data)
#print n,
if n == -1: break
fp.write(data, 0, n)
#print
def close(self):
pass
class ZipOutput(DirectoryOutput):
def __init__(self, filename):
self.zipfile = ZipOutputStream(FileOutputStream(filename))
def getName(self, name):
return string.join(string.split(name, '.'), '/')+'.class'
def getFile(self, name):
fname = self.getName(name)
self.zipfile.putNextEntry(ZipEntry(fname))
return self.zipfile
def close(self):
self.zipfile.close()
try:
sys.add_package('com.ms.util.cab')
from com.ms.util import cab
import com.ms.util.cab.CabCreator
from java.util import Date
class CabOutput(cab.CabProgressInterface):
def progress(self, ptype, val1, val2, data):
pass
#print 'cab progress made'
def __init__(self, filename):
self.cabfile = cab.CabCreator(self)
self.cabfile.create(FileOutputStream(filename))
folder = cab.CabFolderEntry()
folder.setCompression(cab.CabConstants.COMPRESSION_LZX, 20)
#print folder.compressionToString()
self.cabfile.newFolder(folder)
def getName(self, name):
return string.join(string.split(name, '.'), '\\')+'.class'
def write(self, name, file):
fname = self.getName(name)
entry = cab.CabFileEntry(name=fname, date=Date())
if isinstance(file, ByteArrayOutputStream):
file = ByteArrayInputStream(file.toByteArray())
elif isinstance(file, type('')):
file = FileInputStream(file)
self.cabfile.addStream(file, entry)
def close(self):
self.cabfile.complete()
except AttributeError:
pass
if __name__ == '__main__':
for of in [CabOutput('c:\\jython\\test.cab')]: #DirectoryOutput('c:\\jython\\dtest'), ZipOutput('c:\\jython\\test.jar')]:
of.write('org.python.core.PyInteger', 'c:\\jython\\JavaCode\\org\\python\\core\\PyInteger.class')
of.write('org.python.core.PyFloat', 'c:\\jython\\JavaCode\\org\\python\\core\\PyFloat.class')
bytes = ByteArrayOutputStream()
bytes.write(jarray.array([10]*500, 'b'))
of.write('hi.there', bytes)
of.close()
|