首页 新闻 会员 周边

关于文件压缩后解压出错的问题

0
[待解决问题]

各位有谁知道这个问题是怎么回事?

每次解压的时候都提示“压缩文件已损坏”。

压缩的文件是多个Excel ,压缩完成后文件只剩下第一个,并且提示“压缩文件已损坏”。

  public static DfsItem CreateZipFileDfsUrl(int tenantId, int userId, List<DfsItem> dfsItems, string ZipedFileName, string Password)
        {
            if (dfsItems == null || dfsItems.Count() <= 0)
            {
                return null;
            }
            if (Path.GetExtension(ZipedFileName) != "zip")
            {
                ZipedFileName = Path.ChangeExtension(ZipedFileName, "zip");
            }
            //string dfspath = "";
            string createZipedFileName = System.Environment.CurrentDirectory + @"\" + Guid.NewGuid();
            ZipOutputStream outStream = new ZipOutputStream(File.Create(createZipedFileName));
            outStream.SetLevel(4);         
         
            if (!string.IsNullOrEmpty(Password.Trim())) outStream.Password = Password.Trim();
            ZipEntry entry = null;
            Crc32 crc = new Crc32();
            byte[] buffer = new byte[4096]; //缓冲区大小

            DfsItem dfsItem = null;
            try
            {
                //创建当前文件夹  
                //entry = new ZipEntry("/");  //加上 “/” 才会当成是文件夹创建  
                //outStream.PutNextEntry(entry);
                //outStream.Flush();
                string fileName = "";

                int i = 0;
                foreach (var item in dfsItems)
                {
                    i++;
                    byte[] fileBuffer = ReadDfsFile(item, ref fileName);
                    entry = new ZipEntry(fileName);
                    entry.DateTime = DateTime.Now;
                    entry.Size = fileBuffer.Length;
                    crc.Reset();
                    crc.Update(fileBuffer);
                    entry.Crc = crc.Value;
                    outStream.PutNextEntry(entry);
                    using (MemoryStream memStream = new MemoryStream(fileBuffer))
                    {
                        int sourceBytes;
                        do
                        {
                            sourceBytes = memStream.Read(buffer, 0, buffer.Length);
                            outStream.Write(buffer, 0, sourceBytes);
                        } while (sourceBytes > 0);
                    }
                               
                }
                Logger.Error("共执行了"+i+"");              //记录执行次数
                outStream.Finish();
                //outStream.IsStreamOwner = true;     //关闭基础流 
                outStream.Close();
                Logger.Error("流已经关闭");

                using (MemoryStream ms = new MemoryStream())
                {
                    using (FileStream fs = File.OpenRead(createZipedFileName))
                    {
                        int sourceBytes;
                        do
                        {
                            sourceBytes = fs.Read(buffer, 0, buffer.Length);
                            ms.Write(buffer, 0, sourceBytes);
                        } while (sourceBytes > 0);
                        dfsItem = new DfsItem(DfsKeySpace, ZipedFileName, (Byte[])ms.GetBuffer(), tenantId);
                    }
                }

            }
            catch (Exception ex)
            {
                Logger.Error("CreateZipFileDfsUrl 出现的异常" + ex.Message);              //catch   CreateZipFileDfsUrl  出现的异常
            }
            finally
            {
                if (entry != null)
                    entry = null;
                GC.Collect();
            }
            if (File.Exists(createZipedFileName))
            {
                File.Delete(createZipedFileName);
            }
            return dfsItem;
        }

 

梦,,,的主页 梦,,, | 菜鸟二级 | 园豆:202
提问于:2017-11-26 17:06
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册