Click here to Skip to main content
15,889,661 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I want to download a folder by compressing it to .tar extension.I have use the ICSharpCode.SharpZipLib.Tar to compress. But it is not working. My code is below.
Please help me out..ASAP

C#
private void CreateTar(string outputTarFilename, string sourceDirectory)
   {
       Response.ClearContent();
       Response.ClearHeaders();
       Response.Clear();
       Response.AddHeader("Content-Disposition", "attachment; filename=" + outputTarFilename);
       Response.ContentType = "application/tar";

       using (FileStream fs = new FileStream(outputTarFilename, FileMode.Create, FileAccess.Write, FileShare.None))
       using (Stream gzipStream = new GZipOutputStream(fs))
       using (TarArchive tarArchive = TarArchive.CreateOutputTarArchive(gzipStream))
       {
           AddDirectoryFilesToTar(tarArchive, sourceDirectory, true);

       }



   }
   private void AddDirectoryFilesToTar(TarArchive tarArchive, string sourceDirectory, bool recurse)
   {
       try
       {
           // Recursively add sub-folders
           if (recurse)
           {
               string[] directories = Directory.GetDirectories(sourceDirectory);
               foreach (string directory in directories)
                   AddDirectoryFilesToTar(tarArchive, directory, recurse);
           }

           // Add files
           string[] filenames = Directory.GetFiles(sourceDirectory);
           foreach (string filename in filenames)
           {
               TarEntry tarEntry = TarEntry.CreateEntryFromFile(filename);
               tarArchive.WriteEntry(tarEntry, true);

           }


          // Response.Flush();
         //  Response.End();
       }
       catch (Exception ex)
       {


       }

   }
Posted
Comments
phil.o 7-Nov-13 8:10am    
'It is not working' is not a valid issue description.
What is the real error message/exception? You may have to do something with ex variable in your catch block, because as it is, you just swallow the exception (loosing the information in the process).

1 solution

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