Read the data from TXT file inside Zip File without extracting the contents in Matlab

大城市里の小女人 提交于 2019-12-12 03:48:06

问题


I have tab delimited ascii data in txt files which are zip compressed (and the zip may or may not contain other files). I would like to read this data into a matrix without uncompressing the zip files.

There were a few similar @matlab / @java posts earlier:

Read the data of CSV file inside Zip File without extracting the contents in Matlab

Extracting specific file from zip in matlab

Read Content from Files which are inside Zip file

I have gotten this far thanks to the above - I can identify the .txt inside the zip, but don't know how to actually read its contents. First example:

zipFilename = 'example.zip';
zipJavaFile = java.io.File(zipFilename);
zipFile=org.apache.tools.zip.ZipFile(zipJavaFile);
entries=zipFile.getEntries;
cnt=1;
while entries.hasMoreElements
    tempObj=entries.nextElement;
    file{cnt,1}=tempObj.getName.toCharArray';
    cnt=cnt+1;
end
ind=regexp(file,'$*.xml$');
ind=find(~cellfun(@isempty,ind));
file=file(ind);
file = cellfun(@(x) fullfile('.',x),file,'UniformOutput',false);
% Now Operate Any thing on File.
zipFile.close

HOWEVER, I found no example as to how to "operate anything on file". I can extract the path within the zip file, but don't know how to actually read the contents of this txt file. (I wish to directly read its contents into memory -- a matrix --, without extraction, if possible.)

The other example is

zipFilename = 'example.zip';
zipFile = org.apache.tools.zip.ZipFile(zipFilename);
entries = zipFile.getEntries;
while entries.hasMoreElements
    entry = entries.nextElement;
    entryName = char(entry.getName);
    [~,~,ext] = fileparts(entryName);
    if strcmp(ext,'.txt')
        inputStream  = zipFile.getInputStream(entry);
        %Read the contents of the file
        inputStream.close;
    end
end
zipFile.close

The original example contained code to extract the file, but I merely want to read it directly into memory. Again, I don't know how exactly to work with this inputStream.

Could anyone give me a suggestion with a MWE?

来源:https://stackoverflow.com/questions/37456361/read-the-data-from-txt-file-inside-zip-file-without-extracting-the-contents-in-m

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!