2

このコードを 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

4

3 に答える 3

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()
于 2008-11-21T08:46:24.483 に答える
1

それはあなたの質問に対する答えではありませんが、関連しています。CPython バージョンは次のとおりです。

from zipfile import ZipFile, ZIP_DEFLATED

def test(file):
    ZipFile(file+".zip", "w", ZIP_DEFLATED).write(file)
于 2008-11-21T11:19:31.897 に答える
0

閉じていることを確認せずに ZipFile を使用しないでください。

with ZipFile('spam.zip', 'w') as myzip:
    myzip.write('eggs.txt')
于 2014-09-04T10:16:30.730 に答える