Missy
Missy

Reputation: 1368

File to Zip Exists C#

I have numerous large files that I have to transfer. My TX program keeps hanging when I zip everything into one large file. So I want to zip the individual files.

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

    void ProcessFiles(string path, string choice="U")
    {
        string[] files;

        files = Directory.GetFiles(path);
        foreach (string file in files)
        {
            // Process each file
            if (!file.ToUpper().Contains("zip"))
            {
                FileInfo fi = new FileInfo(file);
                string startPath = fi.DirectoryName;
                string zipPath = fi.DirectoryName + @"\zipped\";
                string zipFile = zipPath + fi.Name+".zip";

                string extractPath = startPath + @"\unzipped";
                if (!Directory.Exists(extractPath))
                {
                    Directory.CreateDirectory(extractPath);
                }

                if (choice == "Z")
                {
                    // zip it --> always give me the "File's In Use Error"
                    // option I had tried but did not work  --> Directory.CreateDirectory(zipPath);
                    System.IO.Compression.ZipFile.CreateFromDirectory(startPath, zipFile);

                }
                else
                {// unzip -- works fine

                    System.IO.Compression.ZipFile.ExtractToDirectory(file, extractPath);
                }

            }

        }

    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.folderBrowserDialog1.ShowDialog();
        ProcessFiles(folderBrowserDialog1.SelectedPath, "U");
    }

    private void button2_Click(object sender, EventArgs e)
    {
        this.folderBrowserDialog1.ShowDialog();
        ProcessFiles(folderBrowserDialog1.SelectedPath,"Z");
    }
   }
 }

I saw the posts about people zipping into their existing files and I don't think that's what I'm doing. I would be so happy and appreciative if someone could help me figure this out.

Upvotes: 0

Views: 990

Answers (1)

Tommaso Belluzzo
Tommaso Belluzzo

Reputation: 23675

Zipping a file into the same folder that you are zipping is what probably causes this exception to be thrown. Try to change your code logics so that the zipped file is created outside the target folder:

// Exception
ZipFile.CreateFromDirectory(@"C:\MyPath", @"C:\MyPath\MyArchive.zip");

// No Exception
ZipFile.CreateFromDirectory(@"C:\MyPath", @"C:\MyOtherPath\MyArchive.zip");

Avoid doing bizarre tricks like copying the target folder into another location and passing that one to the CreateFromDirectory method. This will slow things down a lot and will occupy unnecessary space on your drive, especially if your folder contains big files.

On a side note, never change your directory paths manually by concatenating strings. This can, sometimes, produce weird results. Use Path.Combine instead, which can internally handle everything much better:

String zipPath = Path.Combine(fi.DirectoryName, @"\zipped\");

Also, refactor your code so that the necessary paths are computed only when a specific action is required. This will slightly increase the process performance:

FileInfo fi = new FileInfo(file);
string startPath = fi.DirectoryName;

if (choice == "Z")
{
    string zipPath = fi.DirectoryName + @"\zipped\";
    string zipFile = zipPath + fi.Name+".zip";

    Directory.CreateDirectory(zipPath);
    ZipFile.CreateFromDirectory(startPath, zipFile);
}
else
{
    string extractPath = startPath + @"\unzipped";

    if (!Directory.Exists(extractPath))
        Directory.CreateDirectory(extractPath);

    ZipFile.ExtractToDirectory(file, extractPath);
}

Upvotes: 1

Related Questions