Matlab 中的 GZIP 用于大文件

GZIP in Matlab for big files

我有一个函数可以解压缩使用 zlib 库(改编自 here)打包的字节数组 Z

我收到神秘错误

'MATLAB array exceeds an internal Java limit.' 

在此代码的第 2nd 行:

import com.mathworks.mlwidgets.io.InterruptibleStreamCopier
a=java.io.ByteArrayInputStream(Z);
b=java.util.zip.GZIPInputStream(a);
isc = InterruptibleStreamCopier.getInterruptibleStreamCopier;
c = java.io.ByteArrayOutputStream;
isc.copyStream(b,c);
M=typecast(c.toByteArray,'uint8');

正在尝试实施 Mark Adler 的建议:

Z=reshape(Z,[],8);
import com.mathworks.mlwidgets.io.InterruptibleStreamCopier
a=java.io.ByteArrayInputStream(Z(:,1));
b=java.util.zip.GZIPInputStream(a);
for ct = 2:8,b.read(Z(:,ct));end
isc = InterruptibleStreamCopier.getInterruptibleStreamCopier;
c = java.io.ByteArrayOutputStream;
isc.copyStream(b,c);

但是在这个 isc.copystream 我得到这个错误:

Java exception occurred:
java.io.EOFException: Unexpected end of ZLIB input stream

    at java.util.zip.InflaterInputStream.fill(Unknown Source)

    at java.util.zip.InflaterInputStream.read(Unknown Source)

    at java.util.zip.GZIPInputStream.read(Unknown Source)

    at java.io.FilterInputStream.read(Unknown Source)

    at com.mathworks.mlwidgets.io.InterruptibleStreamCopier.copyStream(InterruptibleStreamCopier.java:72)

    at com.mathworks.mlwidgets.io.InterruptibleStreamCopier.copyStream(InterruptibleStreamCopier.java:51)

直接从文件读取 我试图直接从文件中读取数据。

streamCopier = com.mathworks.mlwidgets.io.InterruptibleStreamCopier.getInterruptibleStreamCopier;
fileInStream = java.io.FileInputStream(java.io.File(filename));
fileInStream.skip(datastart);
gzipInStream = java.util.zip.GZIPInputStream( fileInStream );
baos = java.io.ByteArrayOutputStream;
streamCopier.copyStream(gzipInStream,baos);
data = baos.toByteArray;
baos.close;
gzipInStream.close;
fileInStream.close;

适用于小文件,但对于大文件我得到:

Java exception occurred:
java.lang.OutOfMemoryError

在第 streamCopier.copyStream(gzipInStream,baos);

不需要一口气看完。您可以重复 b.read() 调用,在其中提供指定长度的字节数组。重复直到 returns -1。使用生成的块在一个巨大的 MATLAB 数组中构建你的结果,而不是在一个巨大的 Java 数组中。

瓶颈似乎是每个正在创建的 Java 对象的大小。这发生在 java.io.ByteArrayInputStream(Z) 中,因为无法将 MATLAB 数组输入 Java w/o 转换中,并且在 copyStream 中也会发生,其中数据实际上被复制到输出中 buffer/memory.我有一个类似的想法,将对象拆分为允许大小的块 (src):

function chunkDunzip(Z)
%% Imports:
import com.mathworks.mlwidgets.io.InterruptibleStreamCopier
%% Definitions:
MAX_CHUNK = 100*1024*1024; % 100 MB, just an example
%% Split to chunks:
nChunks = ceil(numel(Z)/MAX_CHUNK);
chunkBounds = round(linspace(0, numel(Z), max(2,nChunks)) );

V = java.util.Vector();
for indC = 1:numel(chunkBounds)-1
  V.add(java.io.ByteArrayInputStream(Z(chunkBounds(indC)+1:chunkBounds(indC+1))));
end

S = java.io.SequenceInputStream(V.elements);  
b = java.util.zip.InflaterInputStream(S);

isc = InterruptibleStreamCopier.getInterruptibleStreamCopier;
c = java.io.FileOutputStream(java.io.File('D:\outFile.bin'));
isc.copyStream(b,c);
c.close();

end

几个注意事项:

  1. 我使用了 FileOutputStream,因为它不会 运行 进入 Java 对象的内部限制(就我的测试而言)。
  2. 仍然需要增加 Java 堆内存。
  3. 我使用 deflate 而非 gzip 对其进行了演示。 gzip 的解决方案非常相似 - 如果这是一个问题,我会修改它。