Untar specific files inside a .tar file

11 visualizaciones (últimos 30 días)
Ernesto Barbazán
Ernesto Barbazán el 2 de Oct. de 2020
Comentada: Mohammad Sami el 5 de Oct. de 2020
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.

Respuesta aceptada

Mohammad Sami
Mohammad Sami el 3 de Oct. de 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 comentarios
Ernesto Barbazán
Ernesto Barbazán el 4 de Oct. de 2020
Editada: Ernesto Barbazán el 4 de Oct. de 2020
Ok, I think I get it now. Thank you for the detailed explanation.
Mohammad Sami
Mohammad Sami el 5 de Oct. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre C Shared Library Integration en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by