Home > Mobile >  java.util.zip.ZipException: no current ZIP entry
java.util.zip.ZipException: no current ZIP entry

Time:05-16

I am trying to create a csv file and zip it. It looks like the reason for this exception is the size (i.e. byte size count of column names in csv) is exceeding 8192 bytes. I would like to know how to increase this limit, is it possible or any other solution. Please suggest.

Thanks in advance.

java.util.zip.ZipException: no current ZIP entry
    at java.util.zip.ZipOutputStream.write(ZipOutputStream.java:326)
    at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)
    at sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:282)
    at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:125)
    at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:135)
    at java.io.OutputStreamWriter.write(OutputStreamWriter.java:220)
    at java.io.Writer.write(Writer.java:157)
    at java.io.Writer.append(Writer.java:269)
    at java.io.Writer.append(Writer.java:50)
    at org.apache.commons.csv.CSVFormat.printWithQuotes(CSVFormat.java:1475)
    at org.apache.commons.csv.CSVFormat.print(CSVFormat.java:1224)
    at org.apache.commons.csv.CSVFormat.print(CSVFormat.java:1210)
    at org.apache.commons.csv.CSVFormat.printRecord(CSVFormat.java:1322)
    at org.apache.commons.csv.CSVPrinter.printRecord(CSVPrinter.java:271)
    at org.apache.commons.csv.CSVPrinter.<init>(CSVPrinter.java:110)
    at com.carrieriq.metriclog.business.transformer.csv.Test.main(Test.java:216)

Test.java

package com.carrieriq.metriclog.business.transformer.csv;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;

import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.zip.ZipOutputStream;


public class Test {
    public static void main(String[] args) {
        try
        {
            String [] csvHeaderElements = {

                   
                    "onethree_onethreeFour0",
                    "onethree_onethreeFour1",
                    "onethree_onethreeFour2",
                    "onethree_onethreeFour3",
                    "onethree_onethreeFour4",
                    "onethree_onethreeFour5",


                    "onethree_onethreeFour6",
                    "onethree_onethreeFour7",
                    "onethree_onethreeFour8",
                    "onethree_onethreeFour9",
                    "onethree_onethreeFour10",


                    "onethree_onethreeFour11",
                    "onethree_onethreeFour12",
                    "onethree_onethreeFour13",
                    "onethree_onethreeFour14",
                    "onethree_onethreeFour15",
                    "onethree_onethreeFour16",
                    "onethree_onethreeFour17",
                    "onetwothree_onetwo0_onetwo_rec0_ack",
                    "onetwothree_onetwo0_onetwo_rec0_nack",
                    "onetwothree_onetwo0_onetwo_rec0_dtx",
                    "onetwothree_onetwo0_onetwo_rec1_ack",
                    "onetwothree_onetwo0_onetwo_rec1_nack",
                    "onetwothree_onetwo0_onetwo_rec1_dtx",
                    "onetwothree_onetwo0_onetwo_rec2_ack",
                    "onetwothree_onetwo0_onetwo_rec2_nack",
                    "onetwothree_onetwo0_onetwo_rec2_dtx",
                    "onetwothree_onetwo0_onetwo_rec3_ack",
                    "onetwothree_onetwo0_onetwo_rec3_nack",
                    "onetwothree_onetwo0_onetwo_rec3_dtx",
                    "onetwothree_onetwo0_onetwo_rec4_ack",
                    "onetwothree_onetwo0_onetwo_rec4_nack",
                    "onetwothree_onetwo0_onetwo_rec4_dtx",
                    "onetwothree_onetwo0_onetwo_rec5_ack",
                    "onetwothree_onetwo0_onetwo_rec5_nack",
                    "onetwothree_onetwo0_onetwo_rec5_dtx",
                    "onetwothree_onetwo0_onetwo_rec6_ack",
                    "onetwothree_onetwo0_onetwo_rec6_nack",
                    "onetwothree_onetwo0_onetwo_rec6_dtx",
                    "onetwothree_onetwo0_onetwo_rec7_ack",
                    "onetwothree_onetwo0_onetwo_rec7_nack",
                    "onetwothree_onetwo0_onetwo_rec7_dtx",
                    "onetwothree_onetwo0_onetwo_rec8_ack",
                    "onetwothree_onetwo0_onetwo_rec8_nack",
                    "onetwothree_onetwo0_onetwo_rec8_dtx",
                    "onetwothree_onetwo0_onetwo_rec9_ack",
                    "onetwothree_onetwo0_onetwo_rec9_nack",
                    "onetwothree_onetwo0_onetwo_rec9_dtx",
                    "onetwothree_onetwo0_onetwo_rec10_ack",
                    "onetwothree_onetwo0_onetwo_rec10_nack",
                    "onetwothree_onetwo0_onetwo_rec10_dtx",
                    "onetwothree_onetwo0_onetwo_rec11_ack",
                    "onetwothree_onetwo0_onetwo_rec11_nack",
                    "onetwothree_onetwo0_onetwo_rec11_dtx",
                    "onetwothree_onetwo0_onetwo_rec12_ack",
                    "onetwothree_onetwo0_onetwo_rec12_nack",
                    "onetwothree_onetwo0_onetwo_rec12_dtx",
                    "onetwothree_onetwo0_onetwo_rec13_ack",
                    "onetwothree_onetwo0_onetwo_rec13_nack",
                    "onetwothree_onetwo0_onetwo_rec13_dtx",
                    "onetwothree_onetwo0_onetwo_rec14_ack",
                    "onetwothree_onetwo0_onetwo_rec14_nack",
                    "onetwothree_onetwo0_onetwo_rec14_dtx",
                    "onetwothree_onetwo0_onetwo_rec15_ack",
                    "onetwothree_onetwo0_onetwo_rec15_nack",
                    "onetwothree_onetwo0_onetwo_rec15_dtx",

                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec0_ack",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec0_nack",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec0_dtx",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec1_ack",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec1_nack",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec1_dtx",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec2_ack",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec2_nack",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec2_dtx",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec3_ack",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec3_nack",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec3_dtx",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec4_ack",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec4_nack",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec4_dtx",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec5_ack",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec5_nack",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec5_dtx",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec6_ack",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec6_nack",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec6_dtx",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec7_ack",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec7_nack",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec7_dtx",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec8_ack",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec8_nack",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec8_dtx",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec9_ack",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec9_nack",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec9_dtx",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec10_ack",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec10_nack",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec10_dtx",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec11_ack",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec11_nack",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec11_dtx",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec12_ack",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec12_nack",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec12_dtx",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec13_ack",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec13_nack",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec13_dtx",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec14_ack",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec14_nack",

                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec14_dtx",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec15_ack",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec15_nack",
                    "onetwothreefour_onetwothreefour_rec0_onetwo0_onetwo_rec15_dtx",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec0_ack",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec0_nack",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec0_dtx",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec1_ack",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec1_nack",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec1_dtx",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec2_ack",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec2_nack",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec2_dtx",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec3_ack",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec3_nack",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec3_dtx",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec4_ack",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec4_nack",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec4_dtx",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec5_ack",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec5_nack",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec5_dtx",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec6_ack",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec6_nack",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec6_dtx",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec7_ack",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec7_nack",

                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec7_dtx",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec8_ack",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec8_nack",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec8_dtx",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec9_ack",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec9_nack",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec9_dtx",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec10_ack",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec10_nack",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec10_dtx",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec11_ack",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec11_nack",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec11_dtx",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec12_ack",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec12_nack",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec12_dtx",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec13_ack",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec13_nack",

                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec13_dtx",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec14_ack",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec14_nack",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec14_dtx",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec15_ack",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec15_nack",
                    "onetwothreefour_onetwothreefour_rec1_onetwo0_onetwo_rec15_dtx",
                    "onetwothreefour_onetwothreefour_rec2_onetwo0_onetwo_rec0_ack",
                    "onetwothreefour_onetwothreefour_rec2_onetwo0_onetwo_rec0_nack"

                    
            };
            ServletOutputStream out = new ServletOutputStream() {
                @Override
                public void write(int b) throws IOException {
                    System.out.println(b);
                }
            };

            int count = 0;
            for(String str : csvHeaderElements) {
                count  = str.length();

            }
            System.out.println("size "   count);

            System.out.println("size of array is "   csvHeaderElements.length);
            ZipOutputStream zos = new ZipOutputStream(out);

            CSVPrinter csvPrinter = new CSVPrinter(
                    new OutputStreamWriter(zos),
                    CSVFormat.EXCEL.withHeader(csvHeaderElements)
                    //CSVFormat.DEFAULT.withHeader(csvHeaderElements)
            );
        } catch (IOException e)
        {
            e.printStackTrace();
        }


    }
}

CodePudding user response:

ZIP streams require an entry - which will set up filename within the ZIP content. Use try with resources to ensure that ZIP output stream is closed correctly. This example defines the content within entry called "somename.xyz":

try(ZipOutputStream zos = new ZipOutputStream(out)) {
    zos.putNextEntry(new ZipEntry("somename.xyz"));

    OutputStreamWriter wr = new OutputStreamWriter(zos);
    CSVPrinter csvPrinter = new CSVPrinter(wr,
      CSVFormat.EXCEL.withHeader(csvHeaderElements)
    );
    wr.flush();
}

Or perhaps you are confusing with GZIP stream which would be simpler to setup, just change ZipOutputStream to GZIPOutputStream:

try(GZIPOutputStream zos = new GZIPOutputStream(out)) {
    OutputStreamWriter wr = new OutputStreamWriter(zos);
    CSVPrinter csvPrinter = new CSVPrinter(wr,
      CSVFormat.EXCEL.withHeader(csvHeaderElements)
    );
    wr.flush();
}

CodePudding user response:

You cannot create a zip by just copying the contents of a CSV file to it, you need to create a zip entry first. So the code should look like this:

String[] csvHeaderElements = { // some headers separated with semicolon
        };
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("C:/Users/TestUser/Downloads/test.zip"))) {
    // create zip entry
    ZipEntry entry = new ZipEntry("test.csv");
    zos.putNextEntry(entry);

    CSVPrinter csvPrinter = new CSVPrinter(new OutputStreamWriter(zos), CSVFormat.EXCEL.builder().setHeader(csvHeaderElements).build());
    // some action with csvPrinter
    // ...

    // close entry
    zos.closeEntry();
}
  •  Tags:  
  • java
  • Related