示例#1
0
        public static void Main(string[] args)
        {
            Console.WriteLine("ScriptCoreLib.Archive.Zip");

            Console.WriteLine("" + Crc32Helper.GetCrc32(1, 2, 3));


            var n = DateTime.Now;

            foreach (var x in n.GetType().GetPropertyGetMethodsForType(typeof(int)))
            {
                Console.WriteLine(x.Name + " " + (int)x.Invoke(n, new object[0]));
            }


            Console.WriteLine("" + ZIPFile.ToMsDosDateTime(DateTime.Now));

            var m = new MemoryStream(File.ReadAllBytes("archive.zip"));
            var r = new BinaryReader(m);


            ZIPFile z = r;

            foreach (var f in z.Entries)
            {
                Console.WriteLine(f.FileName + " " + f.Data.Length + " bytes");
                Console.WriteLine(Encoding.ASCII.GetString(f.Data.ToArray()));
                Console.WriteLine();
            }

            var zz = new ZIPFile
            {
                { "dynamic0.txt", "zip!" },
                { "dynamic1.txt", "hey" },
            };

            zz.Add("dynamic2.txt", 0x30, 0x31, 0x32, 0xff);
            zz.Add("archive.zip", File.ReadAllBytes("archive.zip"));

            var zzm = new MemoryStream();

            using (var w = new BinaryWriter(zzm))
            {
                zz.WriteTo(w);
            }

            File.WriteAllBytes("dynamic1.zip", zzm.ToArray());
        }
        public void WriteTo(BinaryWriter w)
        {
            // http://www.pkware.com/documents/casestudies/APPNOTE.TXT

            var offsets = new Queue();
            var Items   = this.Entries;

            #region Local file header:
            foreach (var v in Items)
            {
                // if we box unit, it will actually represent Long
                // this is for tostring functionality

                offsets.Enqueue((uint)w.BaseStream.Position);

                //var h = v.Header;
                var file_name = Encoding.ASCII.GetBytes(v.FileName);

                //        local file header signature     4 bytes  (0x04034b50)
                w.Write(ZIPFileEntryHeader.FileHeader);
                //        version needed to extract       2 bytes
                w.Write((short)0x000A);
                //        general purpose bit flag        2 bytes
                w.Write((short)0);

                //        compression method              2 bytes
                w.Write(ZIPFileEntryHeader.UNCOMPRESSED);
                //        last mod file time              2 bytes
                //        last mod file date              2 bytes
                w.WriteUInt32(ToMsDosDateTime(DateTime.Now));

                //        crc-32                          4 bytes
                w.WriteUInt32((uint)Crc32Helper.GetCrc32(v.Data.ToArray()));

                //        compressed size                 4 bytes
                w.WriteUInt32((uint)v.Data.Length);
                //        uncompressed size               4 bytes
                w.WriteUInt32((uint)v.Data.Length);


                //        file name length                2 bytes
                w.Write((short)file_name.Length);

                //        extra field length              2 bytes
                w.Write((short)0);

                //        file name (variable size)
                w.Write(file_name);

                //        extra field (variable size)

                v.Data.WriteTo(w.BaseStream);
            }
            #endregion

            var p = w.BaseStream.Position;


            #region Central directory structure
            foreach (var v in Items)
            {
                //var h = v.Header;
                var offset = (uint)offsets.Dequeue();

                var file_name = Encoding.ASCII.GetBytes(v.FileName);


                //       central file header signature   4 bytes  (0x02014b50)
                w.Write((int)0x02014b50);
                //       version made by                 2 bytes
                w.WriteUInt16((ushort)0x0014);
                //       version needed to extract       2 bytes
                w.WriteUInt16((ushort)0x000A);
                //       general purpose bit flag        2 bytes
                w.WriteUInt16((ushort)0x0000);
                //       compression method              2 bytes
                w.Write(ZIPFileEntryHeader.UNCOMPRESSED);
                //       last mod file time              2 bytes
                //       last mod file date              2 bytes
                w.WriteUInt32(ToMsDosDateTime(DateTime.Now));
                //       crc-32                          4 bytes
                w.WriteUInt32((uint)Crc32Helper.GetCrc32(v.Data.ToArray()));
                //       compressed size                 4 bytes
                w.WriteUInt32((uint)v.Data.Length);
                //       uncompressed size               4 bytes
                w.WriteUInt32((uint)v.Data.Length);
                //       file name length                2 bytes
                w.WriteUInt16((ushort)file_name.Length);
                //       extra field length              2 bytes
                w.WriteUInt16((ushort)0x0000);
                //       file comment length             2 bytes
                w.WriteUInt16((ushort)0x0000);
                //       disk number start               2 bytes
                w.WriteUInt16((ushort)0x0000);
                //       internal file attributes        2 bytes
                w.WriteUInt16((ushort)0x0000);
                //       external file attributes        4 bytes
                w.Write((int)0x0000);
                //       relative offset of local header 4 bytes
                w.WriteUInt32((uint)offset);
                //       file name (variable size)
                w.Write(file_name);
                //       extra field (variable size)
                //       file comment (variable size)
            }
            #endregion

            //

            var q = w.BaseStream.Position;
            var z = q - p;

            #region I.  End of central directory record:
            //end of central dir signature    4 bytes  (0x06054b50)
            w.WriteUInt32((uint)0x06054b50);

            //number of this disk             2 bytes
            w.WriteUInt16((ushort)0);
            //number of the disk with the
            //start of the central directory  2 bytes
            w.WriteUInt16((ushort)0);
            //total number of entries in the
            //central directory on this disk  2 bytes
            w.WriteUInt16((ushort)Items.Length);
            //total number of entries in
            //the central directory           2 bytes
            w.WriteUInt16((ushort)Items.Length);
            //size of the central directory   4 bytes
            w.WriteUInt32((uint)z);
            //offset of start of central
            //directory with respect to
            //the starting disk number        4 bytes
            w.WriteUInt32((uint)p);
            //.ZIP file comment length        2 bytes
            w.WriteUInt16((ushort)0);
            //.ZIP file comment       (variable size)
            #endregion
        }