File: ZipCentralDirectoryEntry.cs

package info (click to toggle)
mono-reference-assemblies 3.12.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 604,240 kB
  • ctags: 625,505
  • sloc: cs: 3,967,741; xml: 2,793,081; ansic: 418,042; java: 60,435; sh: 14,833; makefile: 11,576; sql: 7,956; perl: 1,467; cpp: 1,446; yacc: 1,203; python: 598; asm: 422; sed: 16; php: 1
file content (60 lines) | stat: -rw-r--r-- 2,939 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
using System.IO;
using System.Text;
using SharpCompress.Common.Zip;
using SharpCompress.Common.Zip.Headers;

namespace SharpCompress.Writer.Zip
{
    internal class ZipCentralDirectoryEntry
    {
        internal string FileName { get; set; }
        internal DateTime? ModificationTime { get; set; }
        internal string Comment { get; set; }
        internal uint Crc { get; set; }
        internal uint HeaderOffset { get; set; }
        internal uint Compressed { get; set; }
        internal uint Decompressed { get; set; }


        internal uint Write(Stream outputStream, ZipCompressionMethod compression)
        {
            byte[] encodedFilename = Encoding.UTF8.GetBytes(FileName);
            byte[] encodedComment = Encoding.UTF8.GetBytes(Comment);

            outputStream.Write(new byte[] {80, 75, 1, 2, 0x3F, 0, 0x0A, 0}, 0, 8);
            HeaderFlags flags = HeaderFlags.UTF8;
            if (!outputStream.CanSeek)
            {
                flags |= HeaderFlags.UsePostDataDescriptor;
                if (compression == ZipCompressionMethod.LZMA)
                {
                    flags |= HeaderFlags.Bit1; // eos marker
                }
            }
            outputStream.Write(BitConverter.GetBytes((ushort) flags), 0, 2);
            outputStream.Write(BitConverter.GetBytes((ushort) compression), 0, 2); // zipping method
            outputStream.Write(BitConverter.GetBytes(ModificationTime.DateTimeToDosTime()), 0, 4);
                // zipping date and time
            outputStream.Write(BitConverter.GetBytes(Crc), 0, 4); // file CRC
            outputStream.Write(BitConverter.GetBytes(Compressed), 0, 4); // compressed file size
            outputStream.Write(BitConverter.GetBytes(Decompressed), 0, 4); // uncompressed file size
            outputStream.Write(BitConverter.GetBytes((ushort) encodedFilename.Length), 0, 2); // Filename in zip
            outputStream.Write(BitConverter.GetBytes((ushort) 0), 0, 2); // extra length
            outputStream.Write(BitConverter.GetBytes((ushort) encodedComment.Length), 0, 2);

            outputStream.Write(BitConverter.GetBytes((ushort) 0), 0, 2); // disk=0
            outputStream.Write(BitConverter.GetBytes((ushort) 0), 0, 2); // file type: binary
            outputStream.Write(BitConverter.GetBytes((ushort) 0), 0, 2); // Internal file attributes
            outputStream.Write(BitConverter.GetBytes((ushort) 0x8100), 0, 2);
                // External file attributes (normal/readable)
            outputStream.Write(BitConverter.GetBytes(HeaderOffset), 0, 4); // Offset of header

            outputStream.Write(encodedFilename, 0, encodedFilename.Length);
            outputStream.Write(encodedComment, 0, encodedComment.Length);

            return (uint) (8 + 2 + 2 + 4 + 4 + 4 + 4 + 2 + 2 + 2
                           + 2 + 2 + 2 + 2 + 4 + encodedFilename.Length + encodedComment.Length);
        }
    }
}