SharpZipLib库压缩的文件夹与高水平compresion和eficient时间的子文件夹 [英] SharpZipLib library Compress a folder with subfolders with high level compresion and eficient time

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

问题描述

我用很多现有的代码,我试图压缩文件夹中的许多方面,但仍然我amhaving问题随着时间的推移和文件夹的大小(大约仍然相同的大小)。
这个代码是从库的来源,仍然不给想要的结果。



 静态无效的主要(字符串[ ]参数)
{
// copyDirectory(@C:\x,@D:\1);
ZipOutputStream邮编=新ZipOutputStream(File.Create(@D:\2.zip));

zip.SetLevel(9);

字符串的文件夹= @D:\music

ZipFolder(文件夹,文件夹压缩);
zip.Finish();
zip.Close();
}

公共静态无效ZipFolder(字符串RootFolder,串CurrentFolder,ZipOutputStream的zstream)
{
的String [] =子文件夹Directory.GetDirectories(CurrentFolder);

的foreach(子文件夹中的文件夹弦)
ZipFolder(RootFolder,文件夹的zstream);

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

如果(relativePath.Length→1)
{
的ZipEntry dirEntry;

dirEntry =新的ZipEntry(relativePath);
dirEntry.DateTime = DateTime.Now;
}

的foreach(在Directory.GetFiles字符串文件(CurrentFolder))
{
AddFileToZip(的zstream,relativePath,文件);
}
}

私有静态无效AddFileToZip(ZipOutputStream的zstream,串relativePath,字符串文件)
{
字节[]缓冲区=新的字节[4096 ];
串fileRelativePath =(relativePath.Length→1 relativePath:的String.Empty?)+ Path.GetFileName(文件);
ZipEntry的入门=新的ZipEntry(fileRelativePath);

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

使用(的FileStream FS = File.OpenRead(文件))
{
INT sourceBytes;


{
sourceBytes = fs.Read(缓冲,0,buffer.Length);
zStream.Write(缓冲液,0,sourceBytes);
}当(sourceBytes大于0);
}
}


解决方案

字符串的文件夹= @D:\music;




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



有限制的压缩算法有多少能做到呢。而更多的压缩总是需要更多的时间。


i used many existing codes and i tried to zip the folder in many ways but still i amhaving 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);
    }
}

解决方案

string folder = @"D:\music";

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库压缩的文件夹与高水平compresion和eficient时间的子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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