Beispiel #1
0
        public void WriteToFile(string filePath)
        {
            using var fileStream   = File.Open(filePath, FileMode.Create);
            using var binaryWriter = new BinaryWriter(fileStream);

            // Archive header
            binaryWriter.Write(new[] { 'A', 'L', 'E', 'X' }); // Magic number
            binaryWriter.Write(01);                           // Version maj
            binaryWriter.Write(00);                           // Version min

            for (int i = 0; i < 12; ++i)
            {
                binaryWriter.Write((byte)0);
            }

            // Archive directory
            binaryWriter.Write(Files.Count);

            List <long> fileLocOffsets = new List <long>();

            foreach (var file in Files)
            {
                binaryWriter.Write(file.FileName); // Length-prefixed automatically
                fileLocOffsets.Add(binaryWriter.BaseStream.Position);
                binaryWriter.Write((long)00);
                binaryWriter.Write((long)file.FileData.Length);
                binaryWriter.Write((int)file.FileCompressionMethod);

                for (int i = 0; i < 8; ++i)
                {
                    binaryWriter.Write((byte)0);
                }
            }

            // File data
            for (int fileIndex = 0; fileIndex < Files.Count; fileIndex++)
            {
                FileArchiveFile file        = Files[fileIndex];
                var             fileDataPos = binaryWriter.BaseStream.Position;

                binaryWriter.BaseStream.Seek(fileLocOffsets[fileIndex], SeekOrigin.Begin);
                binaryWriter.Write(fileDataPos);

                binaryWriter.BaseStream.Seek(fileDataPos, SeekOrigin.Begin);;

                for (int i = 0; i < 4; ++i)
                {
                    binaryWriter.Write((byte)0);
                }

                if (file.FileCompressionMethod == CompressionMethod.None)
                {
                    binaryWriter.Write(file.FileData);
                }
                else if (file.FileCompressionMethod == CompressionMethod.Zstd)
                {
                    using var memoryStream          = new MemoryStream();
                    using var compressStream        = new ZstandardStream(memoryStream, System.IO.Compression.CompressionMode.Compress);
                    compressStream.CompressionLevel = 5;
                    compressStream.Write(file.FileData, 0, file.FileData.Length);
                    compressStream.Close();

                    var compressedData = memoryStream.ToArray();
                    binaryWriter.Write(compressedData);
                }

                for (int i = 0; i < 4; ++i)
                {
                    binaryWriter.Write((byte)0);
                }
            }
        }
Beispiel #2
0
 public void AddFile(FileArchiveFile file)
 {
     throw new NotImplementedException();
 }