2008-11-21 68 views
2

如何將此代碼轉換爲jython?幫我翻譯使用字節的Java代碼到jython代碼

 ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file + ".zip")); 
    byte[] buf = new byte[1024]; 
    int len; 
    //Create a new Zip entry with the file's name. 
    ZipEntry zipEntry = new ZipEntry(file.toString()); 
    //Create a buffered input stream out of the file 
    //we're trying to add into the Zip archive. 
    FileInputStream fin = new FileInputStream(file); 
    BufferedInputStream in = new BufferedInputStream(fin); 
    zos.putNextEntry(zipEntry); 
    //Read bytes from the file and write into the Zip archive. 
    while ((len = in.read(buf)) >= 0) { 
     zos.write(buf, 0, len); 
    } 
    //Close the input stream. 
    in.close(); 
    //Close this entry in the Zip stream. 
    zos.closeEntry(); 

這是我有什麼,但它沒有嚴重

  buf=None          <<<< ? 
      len=None          <<<< ? 
      zipEntry=ZipEntry(file.toString()) 
      fin=FileInputStream(file) 
      bin=BufferedInputStream(fin) 
      self._zos.putNextEntry(zipEntry) 
      while (len=bin.helpme_im_dying(buf)) >= 0): <<<< ? 
       self._zos.write(buf,0,len)    <<<< ? 
       len = bin.read(buf)      <<<< ? 
      bin.close() 
      self._zos.closeEntry() 

參閱本頁面信息https://www.acm.org/crossroads/xrds6-3/ovp63.html

+1

爲什麼當你無論如何使用它一大塊在一個時間緩衝的流? – 2008-11-26 13:08:10

回答

4

下面是函數的精確轉換(除,像你的情況下,使用bin而不是保留關鍵字in)。

from jarray import zeros 
from java.io import BufferedInputStream, FileInputStream, FileOutputStream 
from java.util.zip import ZipEntry, ZipOutputStream 

def test(file): 
    zos = ZipOutputStream(FileOutputStream(file + ".zip")) 
    buf = zeros(1024, 'b') 
    zipEntry = ZipEntry(file) 
    fin = FileInputStream(file) 
    bin = BufferedInputStream(fin) 
    zos.putNextEntry(zipEntry) 
    len = bin.read(buf) 
    while len >= 0: 
     zos.write(buf, 0, len) 
     len = bin.read(buf) 
    bin.close() 
    zos.closeEntry() 
+0

不錯,我從來不知道有關jarray。謝謝克里斯我真的很感激它 – Setori 2008-11-21 08:51:33

+0

我不知道關於jarray,直到你問你的問題。 :-P我在這裏找到它:http://www.jython.org/Project/userguide.html – 2008-11-21 09:03:24

1

這不是你的問題的答案,而是相關的。這裏是一個CPython的版本:

from zipfile import ZipFile, ZIP_DEFLATED 

def test(file): 
    ZipFile(file+".zip", "w", ZIP_DEFLATED).write(file) 
0

不要不保證它是封閉使用的ZipFile:

with ZipFile('spam.zip', 'w') as myzip: 
    myzip.write('eggs.txt')