0

I'm creating a xlsx using poi and saving it on fileSystem. I need to download the file on a servlet call and due to memory constraints I did not create a xssf workbook object and used the following code instead :

byte[] buf = new byte[1024];
ServletOutputStream sOut = response.getOutputStream(); 
FileInputStream input = null;
try {
    long length = fileToRead.length(); 
    input = new FileInputStream(fileToRead);

    while ((input != null) && ((length = input.read(buf)) != -1)) {   
          sOut.write(buf, 0, (int) length);  
    }

Where fileToRead is the file present at the file system.

How can I integrate this with How to create a zip file in Java

Community
  • 1
  • 1
hbabbar
  • 947
  • 4
  • 15
  • 33

1 Answers1

0

You could use

ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());

....
ze = new ZipEntry("xlsData");
zos.putEntry (ze);
// loop
zos.write(buf, 0, (int) length);  

// finally 
zos.close();
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64