gpt4 book ai didi

c# - SharpZipLib 库压缩一个文件夹与子文件夹具有高水平的压缩和高效的时间

转载 作者:行者123 更新时间:2023-11-30 13:04:41 28 4
gpt4 key购买 nike

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

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

最佳答案

string folder = @"D:\music";

如果您正在尝试压缩 MP3 文件,您将不会看到太多压缩。

无论如何,压缩算法的作用是有限的。而且更多的压缩总是需要更多的时间。

关于c# - SharpZipLib 库压缩一个文件夹与子文件夹具有高水平的压缩和高效的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7977668/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com