Yona Kim
Yona Kim

Reputation: 11

C# compress with .tar

I am trying to do programming to compress with .tar and the source code I do programming is the example provided at Microsoft homepage. but there is an error. I don't know why, everything is same as an example at Microsoft homepage.

error is

System.NotSupportedException: The given path's format is not supported.

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

                 //this directory is what I wanna compress..
            string directoryPath = @"C:\\sfdsf";
            DirectoryInfo directorySelected = new DirectoryInfo(directoryPath);


            foreach (FileInfo filetoCompress in directorySelected.GetFiles())
            {
                Compress(filetoCompress);              
            }
        }

        public static void Compress(FileInfo fileToCompress)
        {
            using (FileStream originalFileStream = fileToCompress.OpenRead())
            {
                if ((File.GetAttributes(fileToCompress.FullName) & FileAttributes.Hidden)
                    != FileAttributes.Hidden & fileToCompress.Extension != ".tar")
                {
                    using (FileStream compressedFileStream = File.Create(DateTime.Now+ ".tar"))
                    {
                        using (DeflateStream compressionStream = new DeflateStream(compressedFileStream, CompressionMode.Compress))
                        {
                            originalFileStream.CopyTo(compressionStream);

                            MessageBox.Show("Compressed" + fileToCompress.Name + "from" + fileToCompress.Length.ToString() + " to" + compressedFileStream.Length.ToString    () +" bytes.");
                        }
                    }
                }
            }

        }
    }
}

Upvotes: 0

Views: 1610

Answers (1)

ProgramFOX
ProgramFOX

Reputation: 6390

Remove the at sign before "C:\\sfdsf" or remove one of the backslashes:

string directoryPath = "C:\\sfdsf";

Or:

string directoryPath = @"C:\sfdsf";

From your comment:

I did debugging. something happened at "using (FileStream compressedFileStream = File.Create(DateTime.Now+ ".tar"))"

After converting a DateTime to a string, the string contains slashes and colons. Slashes and colons are invalid characters in file paths. Try this:

string filename = DateTime.Now.ToString.Replace("/", "").Replace(":", "") + ".tar";
using (FileStream compressedFileStream = File.Create(filename))

Upvotes: 2

Related Questions