Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am in trouble of extracting a specific file from a .tar.gz file.
I want to extract one known file from a.tar.gz file. I know the file(known.txt) exist in that .tar.gz(test.tar.gz) file, so i just want to extract the file to temporary location without any search or going through the entire file.

I can able to extract one file from .zip file but when I use same code to extract from .tar.gz file it gives me error "End of Central Directory record could not be found."

I understand the .zip dll will not help to extract file from .tar.gz.

It would be great if someone help me on this one.

Regards, Surya
Posted
Comments
Surya(10980329) 19-Aug-14 8:52am    
Can anyone please help me on this request ?

1 solution

TAR files are just one big file of all your other files appended together, GZ just compresses that 1 file, so you have to decompress all the file to extract what you want (up to the start of the file you want of course) so you can't just extract the file you want since the format does not allow it.

Try using a component like : http://icsharpcode.github.io/SharpZipLib/[^]
 
Share this answer
 
Comments
Surya(10980329) 20-Aug-14 3:07am    
Thanks Mehdi for getting back.

I am currently using below code to search a particular file from the .tar.gz file but it is taking too much time to finish the search and my page getting timeout.

My main objective is to search a file in the .tar.gz file, if available then only extract else not.

Can you please guide me here on how I can fast the search in minimum time ?

Thanks in advance.

Regards, Surya

//Code

using (TarInputStream tarIn = new TarInputStream(new GZipInputStream(File.OpenRead(filename[0]))))
{
TarEntry theEntry;
while ((theEntry = tarIn.GetNextEntry()) != null)
{

if (theEntry.IsDirectory)
{
continue;
}
else
{

if (!string.IsNullOrEmpty(theEntry.Name) && !(theEntry.IsDirectory))
{

if (theEntry.Name.IndexOf(ZipFileName) > 0)
{
string strNewFile = @"" + OutputPath + @"\" + ZipFileName;
if (File.Exists(strNewFile))
{
continue;
}

using (FileStream streamWriter = File.Create(strNewFile))
{
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = tarIn.Read(data, 0, data.Length);
if (size > 0)
streamWriter.Write(data, 0, size);
else
break;
}
streamWriter.Close();
}
}
}
}

}
tarIn.Close();
}
Mehdi Gholam 20-Aug-14 12:39pm    
Like I said you must decompress all the TAR file to get at the file you want, try the library or switch to another compressed format like ZIP.
Surya(10980329) 4-Sep-14 3:00am    
Hi Mehdi,

Good day. Need your help on full extract of a .tar.gz file.

I am using below code, but at point of extract to destination folder(line 4) it is giving me the error "Error base InputStream GZIP header, first byte doesn't match"

Stream inStream = File.OpenRead(gzArchiveName);
Stream gzipStream = new GZipInputStream(inStream);

TarArchive tarArchive = TarArchive.CreateInputTarArchive(gzipStream);
tarArchive.ExtractContents(destFolder);
tarArchive.CloseArchive();

gzipStream.Close();
inStream.Close();

Can you please help on this one ?

Thanks in advance.

Regards, Surya
Mehdi Gholam 4-Sep-14 6:14am    
You TAR first then GZIP it, and GUNZIP then UNTAR.
Surya(10980329) 4-Sep-14 7:01am    
Could you please share me some code for reference ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900