SharpZipLib库压缩具有高级压缩和高效时间的子文件夹的文件夹

时间:2011-11-02 08:48:02

标签: c# performance compression

我使用了很多现有代码,我试图以多种方式压缩文件夹,但我仍然遇到时间和文件夹大小问题(仍然大致相同)。 此代码来自库的源代码,但仍未提供所需结果

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);
    }
}

1 个答案:

答案 0 :(得分:0)

  

string folder = @“D:\ music”;

如果你试图压缩MP3文件,你不会看到太多缩小。

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