【问题标题】:Delete original file after zipping it压缩后删除原始文件
【发布时间】:2018-03-28 15:23:53
【问题描述】:

我正在遍历一个目录并压缩其中的文件。在以下代码中,我将原始文件添加到 zipOutputStream,然后删除该文件。 使用下面的代码,我得到以下异常:

    Exception thrown: 'System.IO.IOException' in mscorlib.dll
System.IO.IOException: The process cannot access the file 'C:\Users\Desktop\test.zip' because it is being used by another process.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
   at System.IO.File.OpenRead(String path)
   at ZipFiles.zipFile(String fileToZip, ZipOutputStream oStream) in C:\Users\Desktop\CodeFile1.cs:line 34
   at ZipFiles.zipByFileSize(String dir) in C:\Users\Desktop\CodeFile1.cs:line 75

我知道这个异常是因为文件被压缩而我同时删除它?如果是这样,我如何确保在删除之前进行压缩?

这是我的代码:

try
                {
                    using (ZipOutputStream oStream = new ZipOutputStream(File.Create(zipPath)))
                    {
                        oStream.SetLevel(8); // 9 = highest compression
                        for (int j = 0; j < i; j++)
                        {
                            string fileToZip = sorted.ElementAt(j);
                            zipFile(fileToZip, oStream);


                            File.Delete(fileToZip);
                        }

                        oStream.Finish();
                        oStream.Close();
                    }


                    // limitIndex = i - 1;
                    startIndex = i - 1;
                    zipNumber += 1;
                    size = 0;
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.ToString());
                } 

我正在使用以下库:

using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;

【问题讨论】:

    标签: c# zip sharpziplib


    【解决方案1】:

    在完成所有压缩后运行第二个循环来执行删除如何?

    try
    {
      using (ZipOutputStream oStream = new ZipOutputStream(File.Create(zipPath)))
      {
        oStream.SetLevel(8); // 9 = highest compression
        for (int j = 0; j < i; j++)
        {
            string fileToZip = sorted.ElementAt(j);
            zipFile(fileToZip, oStream);
        }
    
        oStream.Finish();
        oStream.Close();
      }
    
      // Delete the files now.
      for (int j = 0; j < i; j++)
      {
        string fileToZip = sorted.ElementAt(j);
        File.Delete(fileToZip);
      }
    
    
      // limitIndex = i - 1;
      startIndex = i - 1;
      zipNumber += 1;
      size = 0;
    }
    catch (Exception ex)
    {
      Debug.WriteLine(ex.ToString());
    } 
    

    【讨论】:

    • 这对我不起作用:当我删除“排序”变量中的文件时,排序的长度发生了变化。我找到了一个解决方法:我检查一个文件是否已经在一个 zip 文件中,然后我删除原始目录中的文件。
    猜你喜欢
    • 2019-02-02
    • 2011-02-17
    • 1970-01-01
    • 1970-01-01
    • 2016-05-02
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 2023-01-08
    相关资源
    最近更新 更多