繁体   English   中英

java.util.zip.ZipException:没有当前 ZIP 条目

[英]java.util.zip.ZipException: no current ZIP entry

我正在尝试创建一个 csv 文件并将其压缩。 我看到的问题是它没有我想要拥有的列,我只看到 164 列带有以下字符串(存在于字符串数组中)。 看起来此异常的原因是大小(即 csv 中列名的字节大小计数)超过 8192 个字节。 如何增加此限制或任何其他解决方案?

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 Test.main(Test.java:216)

示例 test.java 文件


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


    }
}

您不能仅通过将 CSV 文件的内容复制到其中来创建 zip,您需要先创建一个 zip 条目。 所以代码应该是这样的:

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

ZIP 流需要一个条目 - 这将在 ZIP 内容中设置文件名。 对资源使用 try 以确保正确关闭 ZIP 输出流。 此示例定义名为“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)
    );
    // ...
    csvPrinter.flush();
}

请注意,OutputStreamWriter 中使用的流编码器的缓冲区大小为 8192,因此您在 8192 处剪切 CSV 列的次要问题可能是您缺少csvPrinter.flush(); 在处理完 CSV 打印之后。

或者您可能对设置更简单的 GZIP 流感到困惑,只需将ZipOutputStream更改为GZIPOutputStream

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM