DotNetZip组件写的方法,压缩成zip文件,打开zip文件里面的word文档会提示不应该显示的对话框

zw520lbzw520lb 2017-04-27 06:28:56
我用这个组件写的压缩成zip文件,当打开zip,然后直接打开里面的word文档之类的,会提出如下的对话框,然后关闭word文档,又会弹出另外一个对话框。 之前是用ICSharpCode.SharpZipLib组件写的,同样的操作,不会弹出任何的对话框。





知道什么原因吗? 不过如果全部解压出来之后再打开的话就没有问题的。

代码如下:

ICSharpCode.SharpZipLib版本的
public static string ZipFiles(string path, string zipFileName)
{
string fileName = string.Empty;

try
{
// Depending on the directory this could be very large and would require more attention
// in a commercial package.
string[] filenames = Directory.GetFiles(path);
fileName = Path.Combine(path, zipFileName + ".zip");

// 'using' statements guarantee the stream is closed properly which is a big source
// of problems otherwise. Its exception safe as well which is great.
using (ZipOutputStream s = new ZipOutputStream(File.Create(fileName)))
{

s.UseZip64 = UseZip64.Off;

s.SetLevel(9); // 0 - store only to 9 - means best compression

byte[] buffer = new byte[4096];

foreach (string file in filenames)
{

// Using GetFileName makes the result compatible with XP
// as the resulting path is not absolute.
ZipEntry entry = new ZipEntry(Path.GetFileName(file));
entry.IsUnicodeText = true;

// Setup the entry data as required.

// Crc and size are handled by the library for seakable streams
// so no need to do them here.

// Could also use the last write time or similar for the file.
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry);

using (FileStream fs = File.OpenRead(file))
{

// Using a fixed size buffer here makes no noticeable difference for output
// but keeps a lid on memory usage.
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
}

// Finish is important to ensure trailing information for a Zip file is appended. Without this
// the created file would be invalid.
s.Finish();
}
}
catch (Exception)
{
// No need to rethrow the exception as for our purposes its handled.
}

return fileName;
}


DotNetZip版本的
 public static string ZipFiles(string path, string zipFileName)
{
Logger.Debug(typeof(CompressionMgr), "ZipFiles");
string fileName = string.Empty;
try
{
// Depending on the directory this could be very large and would require more attention
// in a commercial package.
string[] fileNames = Directory.GetFiles(path);
fileName = Path.Combine(path, zipFileName + ".zip");

using (ZipFile zip = new ZipFile(Encoding.Default))
{
foreach (string file in fileNames)
{
zip.AddFile(file, "");
}
zip.Save(fileName);
}
}
catch (Exception)
{
// No need to rethrow the exception as for our purposes its handled.
}

return fileName;
}
...全文
523 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zw520lbzw520lb 2017-05-03
  • 打赏
  • 举报
回复
这样写就没问题了,看来还是和写法有关
public static string ZipFiles(string path, string zipFileName)
        {
            string fileName = string.Empty;

            try
            {
                // Depending on the directory this could be very large and would require more attention
                // in a commercial package.
                string[] filenames = Directory.GetFiles(path);
                fileName = Path.Combine(path, zipFileName + ".zip");

                // 'using' statements guarantee the stream is closed properly which is a big source
                // of problems otherwise.  Its exception safe as well which is great.
                using (ZipOutputStream s = new ZipOutputStream(File.Create(fileName)))
                {
                    s.AlternateEncoding = Encoding.UTF8;
                    s.AlternateEncodingUsage = ZipOption.AsNecessary;
                    s.EnableZip64 = Zip64Option.Never;
                    s.CompressionLevel = CompressionLevel.Level9; // 0 - store only to 9 - means best compression

                    byte[] buffer = new byte[4096];

                    foreach (string file in filenames)
                    {
                        // Using GetFileName makes the result compatible with XP
                        // as the resulting path is not absolute.                                   
                        s.PutNextEntry(Path.GetFileName(file));
                        using (FileStream fs = File.OpenRead(file))
                        {
                            // Using a fixed size buffer here makes no noticeable difference for output
                            // but keeps a lid on memory usage.
                            int sourceBytes;
                            do
                            {
                                sourceBytes = fs.Read(buffer, 0, buffer.Length);
                                s.Write(buffer, 0, sourceBytes);//write bytes
                            } while (sourceBytes > 0);
                        }
                    }
                }
            }
            catch (Exception)
            {                
            }

            return fileName;
        }
zw520lbzw520lb 2017-04-28
  • 打赏
  • 举报
回复
问题是我打开就马上报这些消息了,没有任何改动文件。同样我自己的电脑上,效果就是不一样。
threenewbee 2017-04-27
  • 打赏
  • 举报
回复
先释放到一个临时目录再打开,避免文件被锁定
stherix 2017-04-27
  • 打赏
  • 举报
回复
和用的解压库无关 因为你双击之后用word打开 关闭后可能修改了文件,rar检测到解压出来的临时文件被改动,会提示你是否把修改后的文件进行压缩,以更新压缩包

62,271

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧