Untar specific files inside a .tar file

조회 수: 12 (최근 30일)
Ernesto Barbazán
Ernesto Barbazán 2020년 10월 2일
댓글: Mohammad Sami 2020년 10월 5일
Hello,
I have a .tar file with over 100 files inside, and I only want to untar 10 of these files (I know their names). The untar function implemented in MATLAB untars all the files contained in the .tar file, and this would be a waste of time with so many files inside as well as if I need to untar other .tar files after it.
My question is similar to the one asked 8 years ago in the post 'untar selcted files from archive'. Is there any way to extract only some especific files inside the .tar file?
I have tried the aproach comented by Walter Roberson, but I think I am doing something wrong. If someone can explain me it a bit more in detail, or come up with another solution and share it, I would appreciate it.
Additionally, I would like to avoid messing up with the original untar function, if posible, and create a new one. I have tried to create a function called myuntar with the same code as the untar function, but it it fails as it is not able to reach the necessary functions the untar function is capable of, and the way to solve it is out of my knowledge.
Thank you in advance.

채택된 답변

Mohammad Sami
Mohammad Sami 2020년 10월 3일
You will have to use java for this.
A minimum working example is as follows (tested on R2020b).
tarpath = fullfile(pwd,'abc.tar'); % change here
file = java.io.File(tarpath);
fis = java.io.FileInputStream(file);
tis = org.apache.commons.compress.archivers.tar.TarArchiveInputStream(fis);
copier = com.mathworks.mlwidgets.io.InterruptibleStreamCopier.getInterruptibleStreamCopier;
extractfolder = fullfile(pwd,'extracthere'); % change here
while true
entry = tis.getNextEntry;
if(isempty(entry))
break;
end
name = char(entry.getName)
if(endsWith(name,'.m')) % change the logic on which files to extract here
path = regexprep(name,'^[\.\\\/]*',''); % remove leading ..\ or ../
path = fullfile(extractfolder,path);
[folder,~,~] = fileparts(path);
mkdir(folder);
outJavaFile = java.io.File(path);
outStream = java.io.FileOutputStream(outJavaFile);
copier.copyStream(tis, outStream);
outStream.close();
end
end
tis.close;
  댓글 수: 4
Ernesto Barbazán
Ernesto Barbazán 2020년 10월 4일
편집: Ernesto Barbazán 2020년 10월 4일
Ok, I think I get it now. Thank you for the detailed explanation.
Mohammad Sami
Mohammad Sami 2020년 10월 5일
The line is to remove ../ or ..\ from the beginning of the path.
This guards against a slip attack, where someone can extract contents into system directory by appending multiple ..\ in front of the path. Whent the fullfile function resolves the path, it will end up resolving to a system folder.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 C Shared Library Integration에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by