6

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

Inbar Rose
  • 41,843
  • 24
  • 85
  • 131
kiki
  • 323
  • 2
  • 5
  • 11
  • This could be useful: http://stackoverflow.com/questions/1350653/c-sharp-sharpziplib-strips-irrelevant-directory-names – Saw Dec 16 '12 at 09:38
  • That is because you are telling it too! Why do you think it do it any way else? – leppie Dec 16 '12 at 09:39
  • I didn't see any other way to add to tar without giving it the full file path. M Sakher Sawan - thank you! it does work on zip files - but i couldn't find a paraplel example for .gz files – kiki Dec 16 '12 at 09:56

1 Answers1

4

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);
Bobson
  • 13,498
  • 5
  • 55
  • 80