Problems creating a Tar Archive with SharpZipLib

  • Thread starter Thread starter Flix
  • Start date Start date
F

Flix

The following piece of code should turn the folder IN and its subfolders
into a single tar file
located in the folder OUT:

using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.Tar;

public static void Create_Tar_Archive(string IN,string OUT)
{
//IN and OUT are directories
if (IN=="" || OUT=="" || !System.IO.Directory.Exists(IN)
|| !System.IO.Directory.Exists(OUT)) return;
string OutName=OUT+"\\"+System.IO.Path.GetFileName(IN)+".tar";

Stream stmout=new FileStream(OutName,FileMode.OpenOrCreate);
TarArchive
TA=ICSharpCode.SharpZipLib.Tar.TarArchive.CreateOutputTarArchive(stmout);
TarEntry TE=TarEntry.CreateEntryFromFile(IN);
TA.WriteEntry(TE,true);
TA.CloseArchive();
stmout.Close();
}

The code seems to work but inside the output tar archive the file paths are
absolute and not relative (they start with C:\...),
and so any attemp to "decompress" the archive into a target folder fails
(illegal paths).

I would like to know how to create a tar archive with relative file paths,
i.e. with a root directory named like System.IO.Path.GetFileName(IN) and the
relative subfolders inside it,
or simply with the files and subfolders of the directory IN in first place
with relative paths.

I don't know how to create the TarEntries to achieve that (the documentation
is not so clear).

Code or links are welcome. Thank you in advance.
 
"Flix" <[email protected]> ha scritto nel messaggio
[......]
I would like to know how to create a tar archive with relative file paths,
i.e. with a root directory named like System.IO.Path.GetFileName(IN) and the
relative subfolders inside it,

I've solved it changing the current directory with
System.IO.Directory.SetCurrentDirectory(new_path) before any operation, and
providing the function with a relative path directory.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top