Example #1
0
        public static uint GetCrc32(params byte[] data)
        {
            var n = new Crc32Helper();

            n.ComputeCrc32(data);

            return(n.Crc32Value);
        }
		public static uint GetCrc32(params byte[] data)
		{
			var n = new Crc32Helper();

			n.ComputeCrc32(data);

			return n.Crc32Value;
		}
Example #3
0
		static void Main(string[] args)
		{
			var crc = new Crc32Helper();
			crc.ComputeCrc32(new byte[] { 1, 2, 0xfe, 0xff });
			
			// Crc32Value = 0x3d414fa9

			var z = new ZIPFile
			{
				{"default.txt", "hello world"}
			};

			var m = z.ToBytes();

			var w = new StringBuilder();

			var xxi = 0;
			foreach (var xx in m)
			{
				xxi++;
				w.Append(" " + xx.ToString("x2"));
				if ((xxi % 16) == 0)
					w.AppendLine();
			}

			Console.WriteLine(w.ToString());

			/*
			 * flash
50 4b 03 04 0a 00 00 00 00 00 80 5c fa 3a 16 ff
ff ff 0b 00 00 00 0b 00 00 00 0b 00 00 00 64 65
66 61 75 6c 74 2e 74 78 74 50 4b 01 02 14 00 0a
00 00 00 00 00 80 5c fa 3a 95 2e 51 ff 29 00 00
00 29 00 00 00 0b 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 64 65 66 61 75 6c 74 2e 74
78 74 50 4b 05 06 00 00 00 00 01 00 01 00 39 00
00 00 29 00 00 00 00 00		
			 * 
			 * .net
50 4b 03 04 0a 00 00 00 00 00 fd 5c fa 3a 85 11
4a 0d 0b 00 00 00 0b 00 00 00 0b 00 00 00 64 65
66 61 75 6c 74 2e 74 78 74 68 65 6c 6c 6f 20 77
6f 72 6c 64 50 4b 01 02 14 00 0a 00 00 00 00 00
fd 5c fa 3a 85 11 4a 0d 0b 00 00 00 0b 00 00 00
0b 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 64 65 66 61 75 6c 74 2e 74 78 74 50 4b 05
06 00 00 00 00 01 00 01 00 39 00 00 00 34 00 00
00 00 00


			 */

			File.WriteAllBytes("archive2.zip", m);
		}
        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 (Entry v in Items)
            {
                var data = v.Data;
                if (data == null)
                {
                    data = new MemoryStream();
                }
                // 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(data.ToArray()));

                //        compressed size                 4 bytes
                w.WriteUInt32((uint)data.Length);
                //        uncompressed size               4 bytes
                w.WriteUInt32((uint)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.Position = 0;
                data.WriteTo(w.BaseStream);
            }
            #endregion

            var p = w.BaseStream.Position;


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

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

                var data = v.Data;
                if (data == null)
                {
                    data = new MemoryStream();
                }

                //       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(data.ToArray()));
                //       compressed size                 4 bytes
                w.WriteUInt32((uint)data.Length);
                //       uncompressed size               4 bytes
                w.WriteUInt32((uint)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
        }