2

how do I translate this code into 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();

this is what I have but it Fails badly

            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()

refer to this page for information https://www.acm.org/crossroads/xrds6-3/ovp63.html

1
  • 1
    Why the buffered stream when you are using it a chunk at a time anyway? Commented Nov 26, 2008 at 13:08

3 Answers 3

4

Here's an exact translation of that function (except, like your case, using bin instead of reserved keyword 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()
Sign up to request clarification or add additional context in comments.

2 Comments

nice i never knew about jarray. thank you Chris I really appreciate it
I didn't know about jarray until you asked your question. :-P I found it here: jython.org/Project/userguide.html
1

It is not an answer to your question, but related. Here is a CPython version:

from zipfile import ZipFile, ZIP_DEFLATED

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

1 Comment

yeah isnt that just great! so smooth
0

Don't use ZipFile without ensuring it is closed:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.