Read files in zip file without unzipping

Hi there
I have a zip file with about 100 binary files in. I have a routine to read the files into matlab variables. Is there a way that I can read the files directly without unpacking the zip file?
Thanks in advance.
Etienne

댓글 수: 1

Jan
Jan 2011년 7월 6일
What do you want to do with a zipped variable? Do you want to unzip one of the files only?

댓글을 달려면 로그인하십시오.

답변 (4개)

Bob Hamans
Bob Hamans 2011년 7월 6일

4 개 추천

Hi Etienne. I don't know if I understand your question but you just want to list the zip file contents without unzipping? The following code does just that:
function filelist = listzipcontents(zipFilename)
% Create a Java file of the ZIP filename.
zipJavaFile = java.io.File(zipFilename);
% Create a Java ZipFile and validate it.
zipFile = org.apache.tools.zip.ZipFile(zipJavaFile);
% Extract the entries from the ZipFile.
entries = zipFile.getEntries;
% Initialize the file list.
filelist={};
% Loop through the entries and add to the file list.
while entries.hasMoreElements
filelist = cat(1,filelist,char(entries.nextElement));
end
end

댓글 수: 5

Bob Hamans
Bob Hamans 2011년 7월 6일
I added some documentation and submitted the function to the File Exchange
Ashish Uthama
Ashish Uthama 2011년 7월 6일
[Move as comment - AU]
Hi Bob
I actualy meant that I would like to read the contents of the files directly without having to unzip the file. It is easy enough to unzip and zip the files. I just wanted to see if there is an alternative.
Regards
Etienne
Oleg Komarov
Oleg Komarov 2014년 5월 13일
편집: Oleg Komarov 2014년 5월 13일
The comment is ambiguous but the code does list the content WITHOUT unzipping.
In other words:
entries = zipFile.getEntries;
does not unzip the content.
The submission can be found on http://www.mathworks.co.uk/matlabcentral/fileexchange/32089-listzipcontents and this should be included with unzip() as an enhancement.
For those using the code snippet above, please add:
zipFile.close
This will "unlock" the java file again. After having some trouble deleting the zip file I found this missing piece of code here:Extracting specific file from zip in matlab - Stack Overflow.
Mark Gugliuzza
Mark Gugliuzza 2023년 3월 21일
Does anyone know if it is possible to perform this recursively? I have zips within zips that I need to read all the file names out of?

댓글을 달려면 로그인하십시오.

Etienne Coetzee
Etienne Coetzee 2011년 7월 6일

1 개 추천

Ok, thanks Robert. I will log it as an enhancement.
Robert Cumming
Robert Cumming 2011년 7월 6일

0 개 추천

have you seen this thread - you could try what it suggests in your own copy of unzip.m - or send an enhancement request to Mathworks.
Its actually quite easy to update the m file(s):
1. Change the number of accepted input arguments to 3.
2. Handle the third input agument (say a cell array of requested files) or set to default (empty cell array)
3. Pass the 3rd argument into the private function extractArchive
4. In the private function do a strcmp on the entryName and your cell array of requested file(s) - if match found extract, if not skip to next (use continue in the while loop)
Brunno Machado de Campos
Brunno Machado de Campos 2018년 1월 11일

0 개 추천

Hi, If anyone is until interested, for zip, on Windows (where I tested):
a = fopen('ZIPFILENAME.zip','r');
c2 = fread(a,'uint8=>uint8');
zipname = char(c2(31:31+c2(27)-1));
zipname = strcat(zipname')

댓글 수: 2

Chris Piker
Chris Piker 2020년 5월 6일
I like it. Thanks! I'll look into the zip binary format a bit more.
Collin Pecora
Collin Pecora 2020년 6월 24일
Allows for extracting specific file from a zip archive.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Scope Variables and Generate Names에 대해 자세히 알아보기

태그

질문:

2011년 7월 5일

댓글:

2023년 3월 21일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by