Exemplo n.º 1
0
        /// <summary>
        /// Creates an RARC virtual folder from data and an archive name.
        /// </summary>
        /// <param name="data">The RARC file data</param>
        /// <param name="name">The name of the RARC archive</param>
        public RARC(byte[] data, string name)
        {
            FileName = name;

            if (Yay0.IsYay0(data))
            {
                data = Yay0.Decompress(data);
            }
            else if (Yaz0.IsYaz0(data))
            {
                data = Yaz0.Decompress(data);
            }

            Header = new RARCHeader(data);
            ReadNodes(data);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates an RARC archive from the given directory and settings.
        /// </summary>
        /// <param name="directory">The directory to be converted to an RARC archive.</param>
        /// <param name="archiveType">The type of archive this will be treated as.</param>
        /// <param name="compressionType">The compression type of the final archive, if one is desired.</param>
        public RARC(string directory, ArchiveType archiveType = ArchiveType.MemoryArchive, CompressionType compressionType = CompressionType.None)
        {
            if (!Directory.Exists(directory))
            {
                throw new ArgumentException("Constructor expects a valid directory!", nameof(directory));
            }

            var archiveName = $"{Path.GetFileName(directory)}.arc";

            if (compressionType != CompressionType.None)
            {
                archiveName += compressionType == CompressionType.SZP ? ".szp" : ".szs";
            }
            var archiveFileName = Path.Combine(Path.GetDirectoryName(directory), archiveName);

            using var writer = new BinaryWriterX(File.Create(archiveFileName), ByteOrder.BigEndian);
            Header           = new RARCHeader();
            CreateArchive(writer, directory, archiveType, compressionType);
            writer.Close();

            // Compress the archive if requested
            if (compressionType != CompressionType.None)
            {
                var fileData = File.ReadAllBytes(archiveFileName);
                switch (compressionType)
                {
                case CompressionType.SZP:
                    var szpData = Yay0.Compress(fileData);
                    if (szpData.Length < fileData.Length)
                    {
                        File.WriteAllBytes(archiveFileName, szpData);
                    }
                    break;

                case CompressionType.SZS:
                    var szsData = Yaz0.Compress(fileData);
                    if (szsData.Length < fileData.Length)
                    {
                        File.WriteAllBytes(archiveFileName, szsData);
                    }
                    break;
                }
            }
        }