Untar specific files inside a .tar file

11 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Mohammad Sami
Mohammad Sami il 3 Ott 2020
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 Commenti
Ernesto Barbazán
Ernesto Barbazán il 4 Ott 2020
Modificato: Ernesto Barbazán il 4 Ott 2020
Ok, I think I get it now. Thank you for the detailed explanation.
Mohammad Sami
Mohammad Sami il 5 Ott 2020
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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su C Shared Library Integration in Help Center e File Exchange

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by