关于SharpZipLib的压缩与解压缩的实现代码,网络上有一堆,千遍一律,连注释也一模一样,一模一样的文章拷来拷去??

我使用SharpZipLib.dll中遇到的问题是:利用SharpZipLib压缩后生成的*.rar文件,利用其可以正常解压,但如果使用文件右击压缩生成的*.RAR文件,在解压过程中出错,具体报错信息:Wrong Local header signature: 0x21726152 ;但*.zip文件可正常解压。

具体压缩、解压代码实现参照网络上的代码,贴出概要代码:

        public bool ZipFile(string dirPath, string zipFilePath, ref string error)
        {         
            
try
            {
                
string[] filenames = Directory.GetFiles(dirPath);
                
using (ZipOutputStream zs = new ZipOutputStream(File.Create(zipFilePath)))
                {
                    zs.SetLevel(
9);
                    
byte[] buffer = new byte[4096];

                    
foreach (string file in filenames)
                    {
                        ZipEntry entry 
= new ZipEntry(Path.GetFileName(file));
                        entry.DateTime 
= DateTime.Now;
                        zs.PutNextEntry(entry);

                        
using (FileStream fs = File.OpenRead(file))
                        {
                            
int sourceBytes;
                            
do
                            {
                                sourceBytes 
= fs.Read(buffer, 0, buffer.Length);
                                zs.Write(buffer, 
0, sourceBytes);
                            }
                            
while (sourceBytes > 0);
                        }
                    }
                    zs.Finish();
                    zs.Flush();
                    zs.Close();
                } 
                
return true;        
            }
            
catch (Exception ex)
            {
                
return false;
            }
           
        }

相关文章:

  • 2021-12-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-30
  • 2021-08-04
  • 2021-09-10
  • 2022-12-23
猜你喜欢
  • 2021-09-14
  • 2022-02-07
  • 2022-12-23
  • 2021-12-19
  • 2021-06-17
  • 2022-01-09
  • 2021-09-30
相关资源
相似解决方案