簡體   English   中英

幫助我將字節轉換為jython代碼來轉換Java代碼

[英]help me translate Java code making use of bytes into jython code

如何將此代碼轉換為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

下面是函數的精確轉換(除,像你的情況下,使用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()

它不是您問題的答案,而是相關的。 這是CPython版本:

from zipfile import ZipFile, ZIP_DEFLATED

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

不要在未確保關閉的情況下使用ZipFile:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM