MATLAB: Selective unzipping of files

extractfileMATLABselectiveunzipzipped

I would like to selectively unzip a file within a large zipped folder without having to unzip the entire folder due to its size. It looks like I can do this through the MATLAB GUI; however, I need to automate this through code. I understand that I can do this in python by loading the zip file as an object and specifying in that object which file we would to exact. Is there a way to do something similar in MATLAB or is there simply an approach that can achieve the same result?

Best Answer

You will need to use java if you wish to extract only specified files
You can refer to java documentations at the following link.
zipFilename = 'abc.zip';
zipJavaFile = java.io.File(zipFilename);
zipFile = org.apache.tools.zip.ZipFile(zipJavaFile);
% entries = zipFile.getEntries; % to get all entries then iterate
entry = zipFile.getEntry('abcfolder/abcfile.txt'); % empty if not found
if(~isempty(entry))
inputstream = zipFile.getInputStream(entry);
outJavaFile = java.io.File(fullfile(pwd,'abcfolder/abcfile.txt'));
outStream = java.io.FileOutputStream(outJavaFile);
copier = com.mathworks.mlwidgets.io.InterruptibleStreamCopier.getInterruptibleStreamCopier;
copier.copyStream(inputstream, outStream);
outStream.close();
end
zipFile.close();