how to unzip files using external unzip software?

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..

 채택된 답변

Friedrich
Friedrich 2014년 1월 9일
편집: Friedrich 2014년 1월 9일

1 개 추천

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

댓글 수: 8

sandy
sandy 2014년 1월 9일
thanks..it works..but this is for one zip file..but i need to do this for 50000 files in a loop..looping is possible in this java code?
Friedrich
Friedrich 2014년 1월 9일
편집: Friedrich 2014년 1월 9일
Sure, do what Walter posted befrore. You do an outer loop around my code and it should work fine. You would also need to adjust the
org.apache.tools.zip.ZipFile('Untitled1.zip');
call and the
fileOutputStream = java.io.FileOutputStream(fullfile(pwd,entryName));
call accordingly. See my updated post above.
sandy
sandy 2014년 1월 9일
i am not familiar with java code...looping also different in java syntax,help file in matlab for java syntax is not sufficient for this i think
sandy
sandy 2014년 1월 9일
편집: sandy 2014년 1월 9일
??? Improper index matrix
reference.
Error in ==> zipfilecode at 6
zipFile =
org.apache.tools.zip.ZipFile(fullfile(zipfolder,zipfiles(i).name));
>>
Friedrich
Friedrich 2014년 1월 9일
편집: Friedrich 2014년 1월 9일
The code works fine for me. From the error it seem that i is not what it is supposed to be. What value does the variable "i" have when the error occurs? And what dimensions has the variable "zipfiles"?
sandy
sandy 2014년 1월 9일
편집: sandy 2014년 1월 9일
i=1 and i made some changes in the code in finding the .zip files using zipsearch() function as u can see below.my .zip files will be present inside many subfolders of a single main folder,so i used zipsearch(). also my output should be in a single folder where all .txt file should stored ther
zipfolder = 'C:\Users\fhempel\Desktop\tmp';
extractionfolder = 'C:\Users\fhempel\Desktop\tmp';
searchstr = '*.zip';
zipfiles = zipsearch(zipfolder, searchstr);
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
I dont know the zipsearch functon and I dont know what it returns. But you need to adjust this line
zipFile = org.apache.tools.zip.ZipFile(fullfile(zipfolder,zipfiles(i).name));
Basically the argument needs to be the absolut path to the zip file you like to process, e.g. C:\some_folder\myzip.zip.
Since the output should be in one folder you can remove this three lines:
if ~exist(fullfile(extractionfolder,zipfiles(i).name(1:end-4)),'dir')
mkdir(fullfile(extractionfolder,zipfiles(i).name(1:end-4)))
end
Finally this one needs to be adjusted:
fileOutputStream = java.io.FileOutputStream(fullfile(extractionfolder,zipfiles(i).name(1:end-4),entryName));
I guess it should be:
fileOutputStream = java.io.FileOutputStream(fullfile(extractionfolder,entryName));
Please also update the extractionfolder variable to an existing folder on your machine. In addition please also update the zipfolder variable accordingly.
Hey, I was searching for this answer!, but I need to just extract the multiple zip files to another targer folder, so what changes should I make? since this will filter out non .txt files..

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

추가 답변 (2개)

Walter Roberson
Walter Roberson 2014년 1월 9일

0 개 추천

댓글 수: 1

sandy
sandy 2014년 1월 9일
편집: sandy 2014년 1월 9일
thanks...this s for fetching files in a dir or a specific folder..fetching inside each zip file is difficult here.any ideas..also.matlab unzip() function is faster than external program zip.exe ?..suggestion

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

Peng Zheng
Peng Zheng 2014년 10월 14일

0 개 추천

Hello, I think this answer is very useful for me.But I don't understand this two lines:
streamCopier = com.mathworks.mlwidgets.io.InterruptibleStreamCopier.getInterruptibleStreamCopier;
streamCopier.copyStream(inputStream, fileOutputStream);
Can you please be so kind to tell me what does this mean and what it will return?

카테고리

도움말 센터File Exchange에서 Environment and Settings에 대해 자세히 알아보기

질문:

2014년 1월 9일

댓글:

2022년 5월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by