示例#1
0
        public static void ExtractBarFile(string inputFile, string file, string outputPath, bool convertFile = true)
        {
            if (string.IsNullOrWhiteSpace(file))
            {
                throw new ArgumentNullException(nameof(file), "Value cannot be null or empty.");
            }

            if (!File.Exists(inputFile))
            {
                throw new FileNotFoundException($"File '{inputFile}' not found!", inputFile);
            }

            if (!outputPath.EndsWith(Path.DirectorySeparatorChar.ToString()))
            {
                outputPath = outputPath + Path.DirectorySeparatorChar;
            }

            BarFile barFilesInfo;

            using (var fileStream = File.Open(inputFile, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                using (var binReader = new BinaryReader(fileStream))
                {
                    //Read Header
                    binReader.BaseStream.Seek(0, SeekOrigin.Begin); //Seek to header
                    var barFileHeader = new BarFileHeader(binReader);

                    //Read Files Info
                    binReader.BaseStream.Seek(barFileHeader.FilesTableOffset, SeekOrigin.Begin); //Seek to file table

                    barFilesInfo = new BarFile(binReader);
                }
            }

            var barFileInfo = barFilesInfo.BarFileEntrys.First(
                key => string.Equals(key.FileName, file, StringComparison.OrdinalIgnoreCase));

            using (var fileStream = File.Open(inputFile, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                using (var binReader = new BinaryReader(fileStream))
                {
                    binReader.BaseStream.Seek(barFileInfo.Offset, SeekOrigin.Begin); //Seek to file

                    var path = Path.Combine(outputPath, barFilesInfo.RootPath,
                                            Path.GetDirectoryName(barFileInfo.FileName) ?? string.Empty);

                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }

                    var filePath = Path.Combine(outputPath, barFilesInfo.RootPath, barFileInfo.FileName);

                    //Extract to tmp file
                    var tempFileName = Path.GetTempFileName();
                    using (var fileStreamFinal =
                               File.Open(tempFileName, FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        using (var final = new BinaryWriter(fileStreamFinal))
                        {
                            var buffer = new byte[4096];
                            int read;
                            var totalread = 0L;
                            while ((read = binReader.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                if (read > barFileInfo.FileSize)
                                {
                                    totalread = barFileInfo.FileSize;
                                    final.Write(buffer, 0, barFileInfo.FileSize);
                                }
                                else if (totalread + read <= barFileInfo.FileSize)
                                {
                                    totalread += read;
                                    final.Write(buffer, 0, read);
                                }
                                else if (totalread + read > barFileInfo.FileSize)
                                {
                                    var leftToRead = barFileInfo.FileSize - totalread;
                                    totalread = barFileInfo.FileSize;
                                    final.Write(buffer, 0, Convert.ToInt32(leftToRead));
                                }

                                if (totalread >= barFileInfo.FileSize)
                                {
                                    break;
                                }
                            }
                        }
                    }

                    //Convert file
                    if (convertFile)
                    {
                        if (L33TZipUtils.IsL33TZipFile(tempFileName) &&
                            !barFileInfo.FileName.EndsWith(".age4scn", StringComparison.OrdinalIgnoreCase))
                        {
                            var rnd           = new Random(Guid.NewGuid().GetHashCode());
                            var tempFileName2 =
                                Path.Combine(Path.GetTempPath(),
                                             $"{Path.GetFileName(barFileInfo.FileName)}-{rnd.Next()}.tmp");
                            L33TZipUtils.ExtractL33TZipFile(tempFileName, tempFileName2);

                            if (File.Exists(tempFileName))
                            {
                                File.Delete(tempFileName);
                            }

                            tempFileName = tempFileName2;
                        }

                        if (barFileInfo.FileName.EndsWith(".xmb", StringComparison.OrdinalIgnoreCase))
                        {
                            try
                            {
                                var rnd           = new Random(Guid.NewGuid().GetHashCode());
                                var tempFileName2 =
                                    Path.Combine(Path.GetTempPath(),
                                                 $"{Path.GetFileName(barFileInfo.FileName)}-{rnd.Next()}.tmp");
                                XmbFileUtils.XmbToXml(tempFileName, tempFileName2);

                                if (File.Exists(tempFileName))
                                {
                                    File.Delete(tempFileName);
                                }

                                tempFileName = tempFileName2;

                                filePath = filePath.Substring(0, filePath.Length - 4);
                            }
                            catch (Exception)
                            {
                                //
                            }
                        }
                        else if (barFileInfo.FileName.EndsWith(".age4scn",
                                                               StringComparison.OrdinalIgnoreCase) &&
                                 !L33TZipUtils.IsL33TZipFile(tempFileName))
                        {
                            var rnd           = new Random(Guid.NewGuid().GetHashCode());
                            var tempFileName2 =
                                Path.Combine(Path.GetTempPath(),
                                             $"{Path.GetFileName(barFileInfo.FileName)}-{rnd.Next()}.tmp");
                            L33TZipUtils.CreateL33TZipFile(tempFileName, tempFileName2);

                            if (File.Exists(tempFileName))
                            {
                                File.Delete(tempFileName);
                            }

                            tempFileName = tempFileName2;
                        }
                    }

                    //Move new file
                    if (File.Exists(filePath))
                    {
                        File.Delete(filePath);
                    }

                    //
                    File.Move(tempFileName, filePath);

                    //
                    File.SetCreationTimeUtc(filePath,
                                            new DateTime(barFileInfo.LastWriteTime.Year, barFileInfo.LastWriteTime.Month,
                                                         barFileInfo.LastWriteTime.Day, barFileInfo.LastWriteTime.Hour,
                                                         barFileInfo.LastWriteTime.Minute, barFileInfo.LastWriteTime.Second));

                    File.SetLastWriteTimeUtc(filePath,
                                             new DateTime(barFileInfo.LastWriteTime.Year, barFileInfo.LastWriteTime.Month,
                                                          barFileInfo.LastWriteTime.Day, barFileInfo.LastWriteTime.Hour,
                                                          barFileInfo.LastWriteTime.Minute, barFileInfo.LastWriteTime.Second));
                }
            }
        }
示例#2
0
        private static async Task ConvertFile(bool convertFile, string tempFileName, BarEntry barFileInfo, string filePath)
        {
            //Convert file
            if (convertFile)
            {
                if (L33TZipUtils.IsL33TZipFile(tempFileName) &&
                    !barFileInfo.FileName.EndsWith(".age4scn", StringComparison.OrdinalIgnoreCase))
                {
                    var rnd           = new Random(Guid.NewGuid().GetHashCode());
                    var tempFileName2 =
                        Path.Combine(Path.GetTempPath(),
                                     $"{Path.GetFileName(barFileInfo.FileName)}-{rnd.Next()}.tmp");
                    await L33TZipUtils.ExtractL33TZipFileAsync(tempFileName, tempFileName2);

                    if (File.Exists(tempFileName))
                    {
                        File.Delete(tempFileName);
                    }

                    tempFileName = tempFileName2;
                }

                if (barFileInfo.FileName.EndsWith(".xmb", StringComparison.OrdinalIgnoreCase))
                {
                    try
                    {
                        var rnd           = new Random(Guid.NewGuid().GetHashCode());
                        var tempFileName2 =
                            Path.Combine(Path.GetTempPath(),
                                         $"{Path.GetFileName(barFileInfo.FileName)}-{rnd.Next()}.tmp");
                        XmbFileUtils.XmbToXml(tempFileName, tempFileName2);

                        if (File.Exists(tempFileName))
                        {
                            File.Delete(tempFileName);
                        }

                        tempFileName = tempFileName2;

                        filePath = filePath.Substring(0, filePath.Length - 4);
                    }
                    catch (Exception)
                    {
                        //
                    }
                }
                else if (barFileInfo.FileName.EndsWith(".age4scn",
                                                       StringComparison.OrdinalIgnoreCase) &&
                         !L33TZipUtils.IsL33TZipFile(tempFileName))
                {
                    var rnd           = new Random(Guid.NewGuid().GetHashCode());
                    var tempFileName2 =
                        Path.Combine(Path.GetTempPath(),
                                     $"{Path.GetFileName(barFileInfo.FileName)}-{rnd.Next()}.tmp");
                    await L33TZipUtils.CompressFileAsL33TZipAsync(tempFileName, tempFileName2);

                    if (File.Exists(tempFileName))
                    {
                        File.Delete(tempFileName);
                    }

                    tempFileName = tempFileName2;
                }
            }

            //Move new file
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }

            File.Move(tempFileName, filePath);

            File.SetCreationTimeUtc(filePath,
                                    new DateTime(barFileInfo.LastWriteTime.Year, barFileInfo.LastWriteTime.Month,
                                                 barFileInfo.LastWriteTime.Day, barFileInfo.LastWriteTime.Hour,
                                                 barFileInfo.LastWriteTime.Minute, barFileInfo.LastWriteTime.Second));

            File.SetLastWriteTimeUtc(filePath,
                                     new DateTime(barFileInfo.LastWriteTime.Year, barFileInfo.LastWriteTime.Month,
                                                  barFileInfo.LastWriteTime.Day, barFileInfo.LastWriteTime.Hour,
                                                  barFileInfo.LastWriteTime.Minute, barFileInfo.LastWriteTime.Second));
        }