简体   繁体   English

档案压缩存在C#

[英]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. 当我将所有内容压缩为一个大文件时,我的TX程序一直挂起。 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. 如果有人能帮助我解决这个问题,我将非常高兴和感激。

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. 避免执行诸如将目标文件夹复制到另一个位置并将该文件夹传递给CreateFromDirectory方法之类的奇怪技巧。 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: 改用Path.Combine ,它可以在内部更好地处理所有事情:

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM