繁体   English   中英

计算SharpZipLib c#中的压缩进度和压缩时间

[英]Calculate the compression progress in SharpZipLib c# and the compression time

我正在使用以下代码,以便使用c#代码从文件夹中压缩文件。

public void zipFile(){

    try 
    {
        // Only get subdirectories that begin with the letter "p."
        string[] dirs = Directory.GetDirectories(path+dataDir);
        //print(path+dataDir);
        Console.WriteLine("The number of directories starting with p is {0}.", dirs.Length);
        foreach (string dir in dirs) 
        {
            outName = dir+".zip";
            FileStream fsOut = File.Create(outName);
            ZipOutputStream zipStream = new ZipOutputStream(fsOut);
            zipStream.SetLevel(7);

            int folderOffset = dir.Length + (dir.EndsWith("\\") ? 0 : 1);

            try{

                CompressFolder(dir, zipStream, folderOffset);
                zipStream.IsStreamOwner = true; // Makes the Close also Close the underlying stream
                zipStream.Close();
            }
            catch(IOException e){

                UnityEngine.Debug.Log (e.Message);
            }
            catch(OutOfMemoryException e){

                UnityEngine.Debug.Log (e.Message);
            }

            Directory.Delete(dir, true);        
        }
    } 
    catch (Exception e) 
    {
        print("The process failed: {0} " + e.ToString());
    }


}   

该代码在dir中搜索文件夹,并将它们一个接一个地压缩。 我有两个问题要问。 首先,为什么压缩性能无法与Winrar相提并论? 我正在使用SharpZipLib。 这两个库有太多区别吗? 如果是,有什么建议可以提高压缩速度? 其次,如何计算foreach内部的压缩进度?

首先,为什么压缩性能无法与Winrar相提并论?

正如您正确观察到的,RAR和ZIP是两种不同的算法。 AFAIK RAR是压缩专有的,而对于解压缩则是免费的。 ZIP在公共领域-免费使用。 当然还有其他差异。

然而,通常,在压缩速度和压缩比之间需要权衡。 ZIP对于压缩率确实非常有用(它可以非常好地压缩文本数据),但是它也是您可以选择的最慢的算法之一。

看看其他一些通用的压缩器:LZMA,LZO,Snappy,LZ4。 后两个速度很快,但通常不会像Lempel-Ziv变体那样压缩数据。

其次,如何计算foreach内部的压缩进度?

您可以首先计算需要压缩的文件或文件夹的数量-这是您的max变量。 在循环中,使用一个current计数器,该计数器在每次迭代时都会递增。 然后(current / (double)max) * 100)给您完成百分比。

同样,根据@Hans Passant注释,您可以包括文件大小以获得更准确的结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM