UnauthorizedAccessException使用Ionic zip捕获文件压缩

时间:2017-04-24 09:06:24

标签: c# ionic-zip

我们的质量保证在生产过程中报告了以下错误:

Access to the path "C: \ ProgramData \ avi \ Logs \ IMPMDesktopClient HTML Debug - Copy - Copy - Copy - Copy - Copy - Copy (2) - Copy.zip" was denied.
At System.IO.__ Error.WinIOError (Int32 errorCode, String maybeFullPath)
   In System.IO.FileStream.Init(String path, FileMode mode, FileAccessAccess, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptionsOptions, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
   At System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
   At Ionic.Zip.ZipEntry._WriteFileData(Stream s)
   At Ionic.Zip.ZipEntry.Write(Stream s)
   At Ionic.Zip.ZipFile.Save()
   At Ionic.Zip.ZipFile.Save(String fileName)
   At IMPMDesktopClient.LogUploader.ZipupLogsForPosting(String strFileTag)
   At IMPMDesktopClient.LogUploader.UploadLogs(TimeSpan timeout, String strTag)

我试过复制案例,但是无法做到。它已在我的机器上成功运行。

目录中有文件夹:Logs和Temp:C:\ ProgramData \ avi。该程序复制并压缩Log文件夹并放置在Temp中。如果压缩文件夹大小超过4 MB(比方说500 MB),它会将zip文件分成多个或多于4 MB的较小zip文件块。

这是我的代码:请让我知道哪里可能出错。

注意:我正在使用Ionic zip库。

private static string ZipupLogsForPosting(string strFileTag)
{
    string strDstPath = Updater.UpdateFolder; 
    string strZipFileName = string.Format("{0}_{1}_{2}_Logs.zip",
                                           Environment.MachineName,
                                           Environment.UserName,
                                           strFileTag.Substring(0, Math.Min(strFileTag.Length, 20)));

    var sb = new StringBuilder(strZipFileName);
    sb.RemoveTheseChars(Path.GetInvalidFileNameChars());
    strZipFileName = sb.ToString();

    string strZipPath = Path.Combine(strDstPath, strZipFileName);

    if (File.Exists(strZipPath))
    {
        if ((File.GetAttributes(strZipPath) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
        {
            File.SetAttributes(strZipPath, FileAttributes.Normal);
        }
        File.Delete(strZipPath);
    }

    Log.Trace("Zipping up logs for posting, Tag='{0}'", strFileTag);
    Log.FlushAllLogs();

    using (var zip = new ZipFile())
    {
        zip.AddDirectory(Log.LogsPath);
        zip.Save(strZipPath);
    }

    return (strZipPath);
}

注意:请不要担心分割zip文件夹。在分割之前尝试在开头压缩文件夹时会出现问题。

0 个答案:

没有答案