System.IO.Compression.ZipArchive适用于大文件

时间:2016-02-16 03:12:55

标签: c# ssis ziparchive

我有a code这是用C#编写的zip文件的SSIS脚本任务。

压缩1gb(approxymately)文件时遇到问题。

我尝试实施this code,但仍然会收到错误' System.OutOfMemoryException'

  

System.OutOfMemoryException:类型的异常' System.OutOfMemoryException'扔了。
         at ST_4cb59661fb81431abcf503766697a1db.ScriptMain.AddFileToZipUsingStream(String sZipFile,String sFilePath,String sFileName,String sBackupFolder,String sPrefixFolder)位于c:\ Users \ dtmp857 \ AppData \ Local \ Temp \ vsta \ 84bef43d323b439ba25df47c365b5a29 \ ScriptMain.cs:第333行
         at ST:4cb59661fb81431abcf503766697a1db.ScriptMain.Main()in c:\ Users \ dtmp857 \ AppData \ Local \ Temp \ vsta \ 84bef43d323b439ba25df47c365b5a29 \ ScriptMain.cs:line 131

这是压缩文件时的代码片段:

protected bool AddFileToZipUsingStream(string sZipFile, string sFilePath, string sFileName, string sBackupFolder, string sPrefixFolder)
{
    bool bIsSuccess = false;

    try
    {
        if (File.Exists(sZipFile))
        {
            using (ZipArchive addFile = ZipFile.Open(sZipFile, ZipArchiveMode.Update))
            {
                addFile.CreateEntryFromFile(sFilePath, sFileName);

                //Move File after zipping it
                BackupFile(sFilePath, sBackupFolder, sPrefixFolder);
            }
       }
       else
       {
       //from https://stackoverflow.com/questions/28360775/adding-large-files-to-io-compression-ziparchiveentry-throws-outofmemoryexception
           using (var zipFile = ZipFile.Open(sZipFile, ZipArchiveMode.Update))
           {
               var zipEntry = zipFile.CreateEntry(sFileName);

               using (var writer = new BinaryWriter(zipEntry.Open()))
               using (FileStream fs = File.Open(sFilePath, FileMode.Open))
               {
                   var buffer = new byte[16 * 1024];

                   using (var data = new BinaryReader(fs))
                   {
                        int read;

                        while ((read = data.Read(buffer, 0, buffer.Length)) > 0)
                            writer.Write(buffer, 0, read);
                   }
               }
          }

          //Move File after zipping it
          BackupFile(sFilePath, sBackupFolder, sPrefixFolder);
      }

      bIsSuccess = true;
  }

  catch (Exception ex)
  {
     throw ex;
  }

  return bIsSuccess;
}

我缺少的,请给我建议可能是教程或处理此问题的最佳实践。

0 个答案:

没有答案