SharpZipLib和WINRAR压缩后,ZIP文件大小不一样

.Net技术 针尖舞 9年前 (2015-08-13) 2313次浏览 0个评论
SharpZipLib压缩 和 WINRAR选择ZIP模式压缩 压缩后大小不一样。压缩出来是改成JAR使用。JAR就是使用ZIP压缩方法,以前我都是用WINRAR选择ZIP模式压缩。

SharpZipLib压缩代码如下:

using System;

using System.IO;

using System.Windows.Forms;

using ICSharpCode.SharpZipLib.Checksums;

using ICSharpCode.SharpZipLib.Zip;

using ICSharpCode.SharpZipLib.GZip;

namespace Nbgzc

{

    class ZipLib

    {

        Crc32 crc;

        ZipOutputStream s;

        string ZipDirectory;

        public void ZipToFile(string _ZipDirectory, string _ZipFile)

        {

            if (!_ZipDirectory.EndsWith(“\”)) _ZipDirectory += “\”;

            ZipDirectory = _ZipDirectory;

            string[] filenames = Directory.GetFiles(_ZipDirectory);

            string[] directorynames = Directory.GetDirectories(_ZipDirectory);

            crc = new Crc32();

            s = new ZipOutputStream(File.Create(_ZipFile));

            s.SetLevel(5); // 0 – store only to 9 – means best compression

            CycleDirectories(_ZipDirectory);

            s.Finish();

            s.Close();

        }

        private void CycleDirectories(string _ZipDirectory)

        {

            string[] filenames = Directory.GetFiles(_ZipDirectory);

            string[] directorynames = Directory.GetDirectories(_ZipDirectory);

            foreach (string file in filenames)

            {

                //MessageBox.Show(file);

                FileStream fs = File.OpenRead(file);

                byte[] buffer = new byte[fs.Length];

                fs.Read(buffer, 0, buffer.Length);

                ZipEntry entry = new ZipEntry(file.Replace(ZipDirectory, “”));

                entry.DateTime = DateTime.Now;

                entry.Size = fs.Length;

                fs.Close();

                crc.Reset();

                crc.Update(buffer);

                entry.Crc = crc.Value;

                s.PutNextEntry(entry);

                s.Write(buffer, 0, buffer.Length);

            }

            foreach (string directory in directorynames)

            {

                CycleDirectories(directory);

            }

        }

    }

}

调用代码:

zl = new ZipLib();

zl.ZipToFile(“Cache/” + BookID.ToString() + “/Jar/”, “Cache/” + BookID.ToString() + “/” + BookID.ToString() + “.jar”);

zl = null;

winrar压缩更好,压缩比更高!

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明SharpZipLib和WINRAR压缩后,ZIP文件大小不一样
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!