コード例 #1
0
        internal void Write(Stream output)
        {
            byte[] buffer = new byte[BLOCK_SIZE];

            WriteOctalBytes(511, buffer, 100, 8); // file mode
            WriteOctalBytes(0, buffer, 108, 8);   // owner ID
            WriteOctalBytes(0, buffer, 116, 8);   // group ID

            //ArchiveEncoding.UTF8.GetBytes("magic").CopyTo(buffer, 257);
            var nameByteCount = ArchiveEncoding.GetEncoding().GetByteCount(Name);

            if (nameByteCount > 100)
            {
                // Set mock filename and filetype to indicate the next block is the actual name of the file
                WriteStringBytes("././@LongLink", buffer, 0, 100);
                buffer[156] = (byte)EntryType.LongName;
                WriteOctalBytes(nameByteCount + 1, buffer, 124, 12);
            }
            else
            {
                WriteStringBytes(ArchiveEncoding.Encode(Name), buffer, 0, 100);
                WriteOctalBytes(Size, buffer, 124, 12);
                var time = (long)(LastModifiedTime.ToUniversalTime() - EPOCH).TotalSeconds;
                WriteOctalBytes(time, buffer, 136, 12);
                buffer[156] = (byte)EntryType;

                if (Size >= 0x1FFFFFFFF)
                {
                    byte[] bytes   = DataConverter.BigEndian.GetBytes(Size);
                    var    bytes12 = new byte[12];
                    bytes.CopyTo(bytes12, 12 - bytes.Length);
                    bytes12[0] |= 0x80;
                    bytes12.CopyTo(buffer, 124);
                }
            }

            int crc = RecalculateChecksum(buffer);

            WriteOctalBytes(crc, buffer, 148, 8);

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

            if (nameByteCount > 100)
            {
                WriteLongFilenameHeader(output);
                // update to short name lower than 100 - [max bytes of one character].
                // subtracting bytes is needed because preventing infinite loop(example code is here).
                //
                // var bytes = Encoding.UTF8.GetBytes(new string(0x3042, 100));
                // var truncated = Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(bytes, 0, 100));
                //
                // and then infinite recursion is occured in WriteLongFilenameHeader because truncated.Length is 102.
                Name = ArchiveEncoding.Decode(ArchiveEncoding.Encode(Name), 0, 100 - ArchiveEncoding.GetEncoding().GetMaxByteCount(1));
                Write(output);
            }
        }
コード例 #2
0
        internal void Write(Stream output)
        {
            byte[] buffer = new byte[512];

            WriteOctalBytes(511, buffer, 100, 8);   // file mode
            WriteOctalBytes(0, buffer, 108, 8);     // owner ID
            WriteOctalBytes(0, buffer, 116, 8);     // group ID

            //Encoding.UTF8.GetBytes("magic").CopyTo(buffer, 257);
            if (Name.Length > 100)
            {
                // Set mock filename and filetype to indicate the next block is the actual name of the file
                WriteStringBytes("././@LongLink", buffer, 0, 100);
                buffer[156] = (byte)EntryType.LongName;
                WriteOctalBytes(Name.Length + 1, buffer, 124, 12);
            }
            else
            {
                WriteStringBytes(Name, buffer, 0, 100);
                WriteOctalBytes(Size, buffer, 124, 12);
                var time = (long)(LastModifiedTime.ToUniversalTime() - Epoch).TotalSeconds;
                WriteOctalBytes(time, buffer, 136, 12);
                buffer[156] = (byte)EntryType;

                if (Size >= 0x1FFFFFFFF)
                {
#if PORTABLE || NETFX_CORE
                    byte[] bytes = BitConverter.GetBytes(Utility.HostToNetworkOrder(Size));
#else
                    byte[] bytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(Size));
#endif
                    var bytes12 = new byte[12];
                    bytes.CopyTo(bytes12, 12 - bytes.Length);
                    bytes12[0] |= 0x80;
                    bytes12.CopyTo(buffer, 124);
                }
            }

            int crc = RecalculateChecksum(buffer);
            WriteOctalBytes(crc, buffer, 148, 8);

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

            if (Name.Length > 100)
            {
                WriteLongFilenameHeader(output);
                Name = Name.Substring(0, 100);
                Write(output);
            }
        }
コード例 #3
0
        internal void Write(Stream output)
        {
            byte[] buffer = new byte[BLOCK_SIZE];

            WriteOctalBytes(511, buffer, 100, 8); // file mode
            WriteOctalBytes(0, buffer, 108, 8);   // owner ID
            WriteOctalBytes(0, buffer, 116, 8);   // group ID

            //ArchiveEncoding.UTF8.GetBytes("magic").CopyTo(buffer, 257);
            if (Name.Length > 100)
            {
                // Set mock filename and filetype to indicate the next block is the actual name of the file
                WriteStringBytes("././@LongLink", buffer, 0, 100);
                buffer[156] = (byte)EntryType.LongName;
                WriteOctalBytes(Name.Length + 1, buffer, 124, 12);
            }
            else
            {
                WriteStringBytes(Name, buffer, 0, 100);
                WriteOctalBytes(Size, buffer, 124, 12);
                var time = (long)(LastModifiedTime.ToUniversalTime() - EPOCH).TotalSeconds;
                WriteOctalBytes(time, buffer, 136, 12);
                buffer[156] = (byte)EntryType;

                if (Size >= 0x1FFFFFFFF)
                {
                    byte[] bytes   = DataConverter.BigEndian.GetBytes(Size);
                    var    bytes12 = new byte[12];
                    bytes.CopyTo(bytes12, 12 - bytes.Length);
                    bytes12[0] |= 0x80;
                    bytes12.CopyTo(buffer, 124);
                }
            }

            int crc = RecalculateChecksum(buffer);

            WriteOctalBytes(crc, buffer, 148, 8);

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

            if (Name.Length > 100)
            {
                WriteLongFilenameHeader(output);
                Name = Name.Substring(0, 100);
                Write(output);
            }
        }
コード例 #4
0
ファイル: TarHeader.cs プロジェクト: rjshaver/sharpcompress
        internal void Write(Stream output)
        {
            if (Name.Length > 255)
            {
                throw new InvalidFormatException("UsTar fileName can not be longer than 255 chars");
            }
            byte[] buffer = new byte[512];
            string name   = Name;

            if (name.Length > 100)
            {
                name = Name.Substring(0, 100);
            }
            WriteStringBytes(name, buffer, 0, 100);

            WriteOctalBytes(511, buffer, 100, 8);
            WriteOctalBytes(0, buffer, 108, 8);
            WriteOctalBytes(0, buffer, 116, 8);
            WriteOctalBytes(Size, buffer, 124, 12);
            var time = (long)(LastModifiedTime.ToUniversalTime() - Epoch).TotalSeconds;

            WriteOctalBytes(time, buffer, 136, 12);

            buffer[156] = (byte)EntryType;

            //Encoding.UTF8.GetBytes("magic").CopyTo(buffer, 257);
            if (Name.Length > 100)
            {
                name = Name.Substring(101, Name.Length);
                ArchiveEncoding.Default.GetBytes(name).CopyTo(buffer, 345);
            }
            if (Size >= 0x1FFFFFFFF)
            {
#if PORTABLE || NETFX_CORE
                byte[] bytes = BitConverter.GetBytes(Utility.HostToNetworkOrder(Size));
#else
                byte[] bytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(Size));
#endif
                var bytes12 = new byte[12];
                bytes.CopyTo(bytes12, 12 - bytes.Length);
                bytes12[0] |= 0x80;
                bytes12.CopyTo(buffer, 124);
            }
            int crc = RecalculateChecksum(buffer);
            WriteOctalBytes(crc, buffer, 148, 8);
            output.Write(buffer, 0, buffer.Length);
        }