1
我需要在5秒内创建100mb的压缩文件,其中包含使用java的CSV文件。我已经创建了包含CSV文件的test.zip,但它花费了太多的时间(〜30秒)来生成zip文件。这里是我到目前为止已经编写的代码:Java创建100MB压缩的csv文件性能问题
ByteArrayOutputStream baos = new ByteArrayOutputStream();
/* Create instance of ZipOutputStream to create ZIP file. */
ZipOutputStream zipOutputStream = new ZipOutputStream(baos);
/* Create ZIP entry for file.The file which is created put into the
* zip file.File is not on the disk, csvFileName indicates only the
* file name to be put into the zip
*/
ZipEntry zipEntry = new ZipEntry("Test.zip");
zipOutputStream.putNextEntry(zipEntry);
/* Create OutputStreamWriter for CSV. There is no need for staging
* the CSV on filesystem . Directly write bytes to the output stream.
*/
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(zipOutputStream, "UTF-8"));
CsvListWriter csvListWriter = new CsvListWriter(bufferedWriter, CsvPreference.EXCEL_PREFERENCE);
/* Write the CSV header to the generated CSV file. */
csvListWriter.writeHeader(CSVGeneratorConstant.CSV_HEADERS);
/* Logic to Write the content to CSV */
long startTime = System.currentTimeMillis();
for (int rowIdx = 0; rowIdx < 7000000; rowIdx++) {
final List<String> rowContent = new LinkedList<String>();
for (int colIdx = 0; colIdx < 6; colIdx++) {
String str = "R" + rowIdx + "C" + colIdx + " FieldContent";
rowContent.add(str);
}
csvListWriter.write(rowContent);
}
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.println("time==" + elapsedTime/1000f + "Seconds");
System.out.println("Size=====" + baos.size()/(Math.pow(1024, 2)) + "MB");
csvListWriter.close();
bufferedWriter.close();
zipOutputStream.close();
baos.close();
我现在用的是超级CSV库,但我也试图在内存中创建的压缩文件,而不无功而返超级CSV库。你能帮我么?
你确定你的机器可以做到吗? Cna你从命令行尝试相同的东西。顺便说一句''mb' ='milli-bits','MB' ='Mega-Bytes' –
而不是建立一个字符串列表,为什么不直接写入ZipOutputStream呢?这会为你节省很多时间。 –
当您对CPU进行配置时,您看到什么并花费最多时间? –