zlib.net压缩返回空数据

时间:2011-10-15 04:52:24

标签: c# zlib

我正在使用c#app自动为更新程序创建一些文件。 updater需要一个zlib数据数组(文件分成2048个字节,然后每个都被压缩)。我可能会遗漏一些小事,但无论如何

这是我的功能

 public bool compress(FileInfo file)
    {
        int max = 0;
        int buffSize = 2048;
        rpPackage Pack = new rpPackage();
        String tmp = "Rohan Patch v1.0";
        Pack.Header = new char[32];
        tmp.CopyTo(0, Pack.Header, 0, tmp.Length);
        if (G.Version != 0)
            Pack.Version = Pack.OVersion = G.Version;
        else
        {
            Console.WriteLine("Please enter patch file version: ");
            G.Version = Convert.ToInt32(Console.ReadLine());
            //G.Version = Pack.Version = Pack.OVersion = 151132417;
        }
        Pack.Type = 10;
        Pack.OModify = file.LastWriteTime;
        Pack.Always1 = 1;
        //Pack.OutputFile = file.Name;
        Pack.OutputFile = new char[64];
        file.Name.CopyTo(0, Pack.OutputFile, 0, file.Name.Length);
        tmp = file.DirectoryName.Replace(Environment.CurrentDirectory, "") + @"\";
        Pack.OutputPath = new char[128];
        if (tmp.Length > 64)
            tmp = tmp.Substring(0, 64);
        tmp.CopyTo(0, Pack.OutputPath, 0, tmp.Length);
        Pack.OutputSize = (int)file.Length;

        if( file.Extension != "" )
            tmp = file.Name.Substring(0, (int)file.Name.Length - ((int)file.Extension.Length)) + "__" + file.Extension.Substring(1, file.Extension.Length - 1) + ".rp";
        else
            tmp = file.Name + "__.rp";

        Pack.RemoteFile = new char[64];
        if (tmp.Length > 64)
            tmp = tmp.Substring(0, 128);
        tmp.CopyTo(0, Pack.RemoteFile, 0, tmp.Length);
        tmp = @"D:\USAPatch\TmpSVN" + new string(Pack.OutputPath).Replace("\0", "").Replace("/", @"\");
        Pack.RemotePath = new char[128];
        if (tmp.Length > 128)
            tmp = tmp.Substring(0, 128);
        tmp.CopyTo(0, Pack.RemotePath, 0, tmp.Length);
        Pack.blobs = new rpBlob[file.Length/2048];
        int blobz = 0;
        FileStream raw = file.OpenRead();
        while( blobz < Pack.blobs.Length )
        {
            byte[] Buff = new byte[buffSize];
            byte[] Buff2 = new byte[buffSize];
            max = raw.Read(Buff, 0, buffSize);
            //Console.ReadKey(true);
            MemoryStream Cstrm = new MemoryStream(Buff2);
            zlib.ZOutputStream gzip = new zlib.ZOutputStream(Cstrm, zlib.zlibConst.Z_DEFAULT_COMPRESSION);
            MemoryStream Dstrm = new MemoryStream(Buff);
            //Console.ReadKey(true);
            //CopyStream(Dstrm, gzip);
            //Dstrm.CopyTo(gzip);
            gzip.Write(Dstrm.ToArray(), 0, (int)Dstrm.Length);

            //gzip.Write(Buff, 0, buffSize);
            Pack.blobs[blobz] = new rpBlob();
            Pack.blobs[blobz].U = max;
            Pack.blobs[blobz].C = (int)Cstrm.Position;
            Pack.blobs[blobz].Blob = new byte[Pack.blobs[blobz].C];
            Pack.blobs[blobz].Blob = Cstrm.ToArray();


            //Console.ReadKey(true);
            blobz++;
            if (max + buffSize > (int)raw.Length)
            {
                buffSize = (max + buffSize) - (int)raw.Length;
                if (buffSize < 0 || (max + buffSize) > (int)raw.Length)
                    break;
            }
        }
        String _path, _file;
        _path = new string(Pack.OutputPath).Replace("\0", ""); _path = _path.TrimEnd();
        _file = new string(Pack.RemoteFile).Replace("\0", ""); ; _file = _file.TrimEnd();

        if (!Directory.Exists(@".\patch\data" + _path))
            Directory.CreateDirectory(@".\patch\data" + _path);
        BinaryWriter cpr = new BinaryWriter(File.OpenWrite(@".\patch\data" + _path + _file));
        cpr.Write(Pack.Header);
        cpr.Write(Pack.Version);
        blobz = 0;
        while (blobz < Pack.blobs.Length )
        {
            cpr.Write(1);
            cpr.Write(Pack.blobs[blobz].U);
            cpr.Write(Pack.blobs[blobz].C);
            cpr.Write(Pack.blobs[blobz].Blob, 0, Pack.blobs[blobz].C);
            blobz++;
        }
        cpr.Write(type(Pack));
        cpr.Write(Pack.Version);
        cpr.Write(Pack.OModify.ToFileTime());
        cpr.Write(1);
        cpr.Write(Pack.OutputFile);
        cpr.Write(Pack.OutputPath);
        cpr.Write(Pack.OutputSize);
        cpr.Write(Pack.RemoteFile);
        cpr.Write(Pack.RemotePath);
        Pack.RemoteSize = (int)cpr.BaseStream.Length + 4;
        cpr.Write(Pack.RemoteSize);
        Upload U = new Upload();
        return U.Update(Pack);
    }

它应该返回像

这样的blob
78 9C 63 60 60 60 B0 60 D0 61 F0 F4 8B 77 0D 71
8E 77 F6 71 0C 8F 77 62 20 06 38 15 E5 67 A7 E6
29 38 E7 24 96 13 A5 9E 81 41 01 4A 33 02 71 08
10 0B 02 B1 08 10 FF 47 03 70 F5 8E BE 5E 20 9C
95 0C B4 23 51 2F BD A4 82 48 8B 80 40 9E 81 9B
78 C5 A3 60 48 00 4B B4 74 EA 4C 94 2E A7 9C D2
BC 12 52 92 29 4A 3A AD 05 12 3E 0C A3 E9 74 14
10 0F AC D0 D2 A9 0B 51 BA 9C 72 F2 F3 53 2A C9
2D 4F AB 98 18 18 EA 19 46 D3 E9 28 20 1E 58 03
D3 29 00 04 9A 5E 09 01 00 00 00 93 00 00 00 00
08 00 00

然而,它将所有blob写为

78 9C 01 00 00 00 00 08 00 00 02 00 00 00

我正在使用zlib.net库

0 个答案:

没有答案