[英]java.util.zip.ZipException: Central Directory Entry not found
[英]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.