Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a .tar.gz archive without folder structure using SharpZipLib

I am trying to .tar.gz a list of files from a folder using SharpZipLib. The problem is no matter how pass the files paths - the result always contains the files paths - and not only the files thems selves. What am i missing here?

string filesFolder = "c:\\testfolder\\test\\";
List<string> filesToZip = new List<string>() { filesFolder +"test1", filesFolder  +"test2"};

using (FileStream fs = new FileStream(filesFolder +"myGz.tar.gz" , FileMode.Create, FileAccess.Write, FileShare.None))
using (Stream gzipStream = new GZipOutputStream(fs))
using (TarArchive tarArchive = TarArchive.CreateOutputTarArchive(gzipStream))
     {
      foreach (string filename in filesToZip )
      {
        {
          TarEntry tarEntry = TarEntry.CreateEntryFromFile(filename);
          tarArchive.WriteEntry(tarEntry, false);
        }
      }
     }

what i get is a "myGz.tar.gz" files. When i try to open it with 7.zip - i get the full folders structure in the archive - c:\testfolder\test\, and in it - "test1", "test".

How do i remove the files paths?

Thanks

like image 408
kiki Avatar asked Dec 16 '12 09:12

kiki


1 Answers

I had the same problem, and I figured it out just after finding this question.

The key is to set the Name property of the tarEntry, before adding it into the archive.

TarEntry tarEntry = TarEntry.CreateEntryFromFile(filename);
tarEntry.Name = Path.GetFileName(filename);
tarArchive.WriteEntry(tarEntry, false);
like image 100
Bobson Avatar answered Oct 29 '22 13:10

Bobson