Read the data from TXT file inside Zip File withou

2019-09-08 09:38发布

问题:

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?