Untar specific files inside a .tar file

11 次查看(过去 30 天)
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