Exemple #1
0
        /// <summary>
        /// Creates an archive out of the specified <see cref="VirtualFilesystemDirectory"/>, optionally compressing the resulting file.
        /// </summary>
        /// <param name="outputPath">Filepath to which to write the file to.</param>
        /// <param name="root"><see cref="VirtualFilesystemDirectory"/> to create an archive out of.</param>
        /// <param name="compression">Optionally compress with Yaz0 or Yay0 compression.</param>
        public static void WriteArchive(string outputPath, VirtualFilesystemDirectory root, ArchiveCompression compression = ArchiveCompression.Uncompressed)
        {
            if (string.IsNullOrEmpty(outputPath))
            {
                throw new ArgumentNullException("filePath", "Cannot write archive to empty file path!");
            }

            if (root == null)
            {
                throw new ArgumentNullException("root", "Cannot write null VirtualFilesystemDirectory to archive.");
            }

            Archive      rarc       = new Archive();
            MemoryStream outputData = new MemoryStream();

            // Create an archive structure from the given root and write it to file. Compression will be applied if specified.
            MemoryStream uncompressedStream = new MemoryStream();

            using (EndianBinaryWriter fileWriter = new EndianBinaryWriter(uncompressedStream, Endian.Big))
            {
                byte[] rawData = rarc.WriteFile(root);

                fileWriter.Write(rawData);
                fileWriter.Seek(0, SeekOrigin.Begin);
                fileWriter.BaseStream.CopyTo(outputData);
            }

            MemoryStream compressedStream = new MemoryStream();

            switch (compression)
            {
            case ArchiveCompression.Yay0:
                throw new NotImplementedException("Yay0 Compression not implemented.");
            //compressedStream = Yay0.Encode(uncompressedStream);
            //break;

            case ArchiveCompression.Yaz0:
                EndianBinaryWriter encoded = Yaz0.Encode(uncompressedStream);
                encoded.Seek(0, SeekOrigin.Begin);
                encoded.BaseStream.CopyTo(compressedStream);
                break;

            case ArchiveCompression.Uncompressed:

                // Well, that was easy.
                compressedStream = uncompressedStream;
                break;
            }

            compressedStream.Seek(0, SeekOrigin.Begin);
            compressedStream.WriteTo(File.Create(outputPath));
        }
Exemple #2
0
        private void Yaz0CompressLittleEndianToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openxmlFileDialog.ShowDialog() == DialogResult.OK)
            {
                FileInfo file = new FileInfo(openyamlFileDialog.FileName);

                saveyaz0FileDialog.ShowDialog();
                if (saveyaz0FileDialog.FileName != "")
                {
                    FileInfo selected = new FileInfo(saveFileDialog.FileName);

                    //TODO: Change to using.
                    FileStream byml = file.OpenRead();
                    File.WriteAllBytes(selected.FullName, Yaz0.Encode(byml));
                    byml.Close();
                    saveyaz0FileDialog.FileName = "";
                }
            }
        }
        /// <summary>
        /// Creates an new file out of the specified <see cref="EndianBinaryWriter"/>, optionally compressing the resulting file.
        /// </summary>
        /// <param name="outputPath">Filepath to which to write the file to.</param>
        /// <param name="stream"><see cref="MemoryStream"/> to create an archive out of.</param>
        /// <param name="compression">Optionally compress with Yaz0 or Yay0 compression.</param>
        public static void SaveFile(string outputPath, MemoryStream stream, ArchiveCompression compression = ArchiveCompression.Uncompressed)
        {
            if (string.IsNullOrEmpty(outputPath))
            {
                throw new ArgumentNullException("filePath", "Cannot write archive to empty file path!");
            }

            if (stream == null)
            {
                throw new ArgumentNullException("root", "Cannot write null EndianBinaryWriter to archive.");
            }

            MemoryStream compressedStream = new MemoryStream();

            switch (compression)
            {
            case ArchiveCompression.Yay0:
                throw new NotImplementedException("Yay0 Compression not implemented.");
            //compressedStream = Yay0.Encode(uncompressedStream);
            //break;

            case ArchiveCompression.Yaz0:
                EndianBinaryWriter encoded = Yaz0.Encode(stream);
                encoded.Seek(0, SeekOrigin.Begin);
                encoded.BaseStream.CopyTo(compressedStream);
                break;

            case ArchiveCompression.Uncompressed:

                // Well, that was easy.
                compressedStream = stream;
                break;
            }

            compressedStream.Seek(0, SeekOrigin.Begin);
            using (var fileStream = File.Create(outputPath))
                compressedStream.WriteTo(fileStream);
        }