MATLAB: How to unzip files using external unzip software

!systemunzipzip

i have 50000 zip files.i need to unzip all those files and want to fetch .txt files alone from each zip files…any help..

Best Answer

Hi,
I would use JAVA to get what you want, e.g. the following example extract the .txt files in a zip files only (done with R2013b):
zipFile = org.apache.tools.zip.ZipFile('Untitled1.zip');
entries = zipFile.getEntries;
while entries.hasMoreElements
entry = entries.nextElement;
entryName = char(entry.getName);
[~,~,ext] = fileparts(entryName);
if strcmp(ext,'.txt')
inputStream = zipFile.getInputStream(entry);
fileOutputStream = java.io.FileOutputStream(fullfile(pwd,entryName));
streamCopier = com.mathworks.mlwidgets.io.InterruptibleStreamCopier.getInterruptibleStreamCopier;
streamCopier.copyStream(inputStream, fileOutputStream);
fileOutputStream.close;
inputStream.close;
end
end
zipFile.close
UPDATED VERSION FOR SCANNING A FOLDER FOR ZIP FILES AND PROCESS ALL OF THEM
zipfolder = 'C:\Users\fhempel\Desktop\tmp';
extractionfolder = 'C:\Users\fhempel\Desktop\tmp';
zipfiles = dir([zipfolder,'\*.zip']);
for i=1:numel(zipfiles)
zipFile = org.apache.tools.zip.ZipFile(fullfile(zipfolder,zipfiles(i).name));
entries = zipFile.getEntries;
while entries.hasMoreElements
entry = entries.nextElement;
entryName = char(entry.getName);
[~,~,ext] = fileparts(entryName);
if strcmp(ext,'.txt')
inputStream = zipFile.getInputStream(entry);
if ~exist(fullfile(extractionfolder,zipfiles(i).name(1:end-4)),'dir')
mkdir(fullfile(extractionfolder,zipfiles(i).name(1:end-4)))
end
fileOutputStream = java.io.FileOutputStream(fullfile(extractionfolder,zipfiles(i).name(1:end-4),entryName));
streamCopier = com.mathworks.mlwidgets.io.InterruptibleStreamCopier.getInterruptibleStreamCopier;
streamCopier.copyStream(inputStream, fileOutputStream);
fileOutputStream.close;
inputStream.close;
end
end
zipFile.close
end