gpt4 book ai didi

java - 如何使用 Commons compress 打包太大并导致内存不足崩溃的文件?

转载 作者:行者123 更新时间:2023-12-02 00:16:38 31 4
gpt4 key购买 nike

在下面的代码中,如果我将 (Apache) Commons 压缩单个文件大小为几 GB,它将崩溃,因为它耗尽了我的所有内存。

我可以让它一次读取然后写入文件的一小部分吗?我一直在研究分块,但我不知道如何做到这一点,以便我可以在将文件写入 .tar 格式后将文件重新组合在一起。

处理此处任何大小的支持文件的最佳方法是什么?

FileOutputStream fileOutputStream = new FileOutputStream("output.tar");
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
GzipCompressorOutputStream gzipOutputStream = new GzipCompressorOutputStream(bufferedOutputStream);
TarArchiveOutputStream tarArchiveOutputStream = new TarArchiveOutputStream(gzipOutputStream)) {

tarArchiveOutputStream.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
tarArchiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

File currentFile = new File("Huge_MultiGB_File.txt");
String relativeFilePath = currentFile.getPath();
TarArchiveEntry tarEntry = new TarArchiveEntry(currentFile, relativeFilePath);
tarEntry.setSize(currentFile.length());
tarArchiveOutputStream.putArchiveEntry(tarEntry);
tarArchiveOutputStream.write(IOUtils.toByteArray(new FileInputStream(currentFile)));
tarArchiveOutputStream.closeArchiveEntry();

最佳答案

您必须写入文件的一小部分并将其写入循环中的输出,而不是首先使用 IOUtils 将整个文件读取到内存

它或多或少是这样完成的:

FileInputStream source=new FileInputStream(....somefile);
tarArchiveOutputStream; prepared to w writing

byte[] buff = new byte[1024*10]; //10kb buff
int numBytesRead = -1; //number of bytes read


while(( numBytesRead = source.read(buff)) > 0 ) {
// while source has bytes, read from source and write
// the same number of bytes to the tar outputstream
tarArchiveOutputStream.write(buff, 0, numBytesRead);
}
}

关于java - 如何使用 Commons compress 打包太大并导致内存不足崩溃的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58085992/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com