0

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.

Missy
  • 1,286
  • 23
  • 52
  • Possible duplicate of [Compress a single file using C#](https://stackoverflow.com/questions/25042141/compress-a-single-file-using-c-sharp) – Missy Feb 04 '18 at 15:41

1 Answers1

1

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);
}
Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98
  • I had tried string zipPath = fi.DirectoryName + @"\zipped\zipped\"; (notice a folder in a folder) but that didn't work. Maybe it is the because it was a subfolder? I will try your suggestion and use a totally different folder. Also, thanks for the advice. :) – Missy Feb 04 '18 at 14:33
  • 1
    You are still trying to pack the folder creating the zip within itself. You must use a totally different folder, as per answer. – Tommaso Belluzzo Feb 04 '18 at 14:34
  • Ah -- I see the problem now. I am wanting to zip individual files and the system wants to zip the entire directory. Any thoughts? – Missy Feb 04 '18 at 14:52
  • Remove the foreach logic for traversing the files and directly call `ZipFile.CreateFromDirectory` on the target directory. – Tommaso Belluzzo Feb 04 '18 at 14:53
  • That will make each file an individually zipped file? I cannot just zip the folder because I cannot transfer such large folders. What if I create a directory for each file and move the file into the directory? – Missy Feb 04 '18 at 14:59
  • https://stackoverflow.com/questions/25042141/compress-a-single-file-using-c-sharp – Tommaso Belluzzo Feb 04 '18 at 15:39