SharpZipLib库使用高级压缩和有效时间压缩带有子文件夹的文件夹 [英] SharpZipLib library Compress a folder with subfolders with high level compresion and efficient time

查看:82
本文介绍了SharpZipLib库使用高级压缩和有效时间压缩带有子文件夹的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了许多现有代码,并且尝试以多种方式压缩文件夹,但是我仍然在时间和文件夹大小(仍然大致相同)方面遇到问题。
,此代码来自库的源代码,仍然没有给出所需的结果

I used many existing codes and I tried to zip the folder in many ways but still I am having problem with time and folder size (still approx same size). this code is from the source of the library and still not giving the wanted result

static void Main(string[] args)
{
    //copyDirectory(@"C:\x", @"D:\1");
    ZipOutputStream zip = new ZipOutputStream(File.Create(@"d:\2.zip"));

    zip.SetLevel(9);

    string folder = @"D:\music";

    ZipFolder(folder, folder, zip);
    zip.Finish();
    zip.Close();
}

public static void ZipFolder(string RootFolder, string CurrentFolder, ZipOutputStream zStream)
{
    string[] SubFolders = Directory.GetDirectories(CurrentFolder);

    foreach (string Folder in SubFolders)
        ZipFolder(RootFolder, Folder, zStream);

    string relativePath = CurrentFolder.Substring(RootFolder.Length) + "/";

    if (relativePath.Length > 1)
    {
        ZipEntry dirEntry;

        dirEntry = new ZipEntry(relativePath);
        dirEntry.DateTime = DateTime.Now;
    }

    foreach (string file in Directory.GetFiles(CurrentFolder))
    {
        AddFileToZip(zStream, relativePath, file);
    }
}

private static void AddFileToZip(ZipOutputStream zStream, string relativePath, string file)
{
    byte[] buffer = new byte[4096];
    string fileRelativePath = (relativePath.Length > 1 ? relativePath : string.Empty) + Path.GetFileName(file);
    ZipEntry entry = new ZipEntry(fileRelativePath);

    entry.DateTime = DateTime.Now;
    zStream.PutNextEntry(entry);

    using (FileStream fs = File.OpenRead(file))
    {
        int sourceBytes;

        do
        {
            sourceBytes = fs.Read(buffer, 0, buffer.Length);
            zStream.Write(buffer, 0, sourceBytes);
        } while (sourceBytes > 0);
    }
}


推荐答案


字符串文件夹= @ D:\音乐;

string folder = @"D:\music";

如果您要压缩MP3文件您不会看到太多收缩。

If you're trying to zip MP3 files you're not going to see much shrinking.

无论如何压缩算法都存在限制。而更多的压缩总是需要更多的时间。

There are limits to how much a compression algorithm can do anyway. And more compression always takes more time.

这篇关于SharpZipLib库使用高级压缩和有效时间压缩带有子文件夹的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆