Esempio n. 1
0
        protected override void DoTaskForFile(string pPath, IVgmtWorkerStruct pInternalNameFileRenamerWorkerStruct,
                                              DoWorkEventArgs e)
        {
            InternalNameFileRenamerWorkerStruct taskStruct = (InternalNameFileRenamerWorkerStruct)pInternalNameFileRenamerWorkerStruct;

            byte[] terminatorBytes = null;
            int    nameLength      = 0;

            if (taskStruct.TerminatorBytes != null)
            {
                terminatorBytes = ByteConversion.GetBytesFromHexString(taskStruct.TerminatorBytes);
            }

            if (!String.IsNullOrEmpty(taskStruct.NameLength))
            {
                nameLength = (int)ByteConversion.GetLongValueFromString(taskStruct.NameLength);
            }

            FileUtil.RenameFileUsingInternalName(pPath,
                                                 ByteConversion.GetLongValueFromString(taskStruct.Offset),
                                                 nameLength,
                                                 terminatorBytes,
                                                 taskStruct.MaintainOriginalExtension);
        }
Esempio n. 2
0
        private void RenameFilesFromExternalList(ExternalListFileRenamerStruct inputValues)
        {
            string fileMask = null;

            string[] sourceFiles;
            int      sourceFileCount;

            long listFileLength;
            long fileNameOffset;

            StringBuilder renameScript;
            StringBuilder undoScript;

            string sourceFile;

            byte[] destinationFileTerminator = new byte[0];
            long   destinationFileBytesSize  = -1;

            byte[] destinationFileBytes;
            string destinationFile;
            string destinationFolder;

            long currentOffset;

            // verify directory exists and list file exists
            if (Directory.Exists(inputValues.SourceFolder) &&
                File.Exists(inputValues.ListFileLocation))
            {
                // set file mask
                fileMask = inputValues.SourceFileMask.Trim();
                fileMask = String.IsNullOrEmpty(fileMask) ? "*.*" : fileMask;

                // get source file count
                sourceFiles     = Directory.GetFiles(inputValues.SourceFolder, fileMask, SearchOption.TopDirectoryOnly);
                sourceFileCount = sourceFiles.GetLength(0);

                // verify some source files exist
                if (sourceFileCount == 0)
                {
                    throw new Exception(String.Format("Source directory is empty."));
                }
                else
                {
                    // convert file name offset to long
                    fileNameOffset = ByteConversion.GetLongValueFromString(inputValues.OffsetToFileNames);

                    // open List File
                    using (FileStream listFs = File.Open(inputValues.ListFileLocation, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        listFileLength = listFs.Length;

                        // verify offset exists within list file
                        if (listFileLength < fileNameOffset)
                        {
                            throw new IOException(String.Format("File name offset is greater than the size of the list file."));
                        }
                        else
                        {
                            renameScript = new StringBuilder();
                            undoScript   = new StringBuilder();

                            // parse file name terminator, if included
                            if (inputValues.FileNameHasTerminator)
                            {
                                try
                                {
                                    destinationFileTerminator = ByteConversion.GetBytesFromHexString(inputValues.FileNameTerminator);
                                }
                                catch (Exception ex1)
                                {
                                    throw new FormatException(String.Format("Unable to convert File Name Terminator Bytes to hex: '{0}'", ex1.Message), ex1);
                                }
                            }
                            else // convert static filename size
                            {
                                try
                                {
                                    destinationFileBytesSize = ByteConversion.GetLongValueFromString(inputValues.FileNameStaticSize);
                                }
                                catch (Exception ex2)
                                {
                                    throw new FormatException(String.Format("Unable to convert File Name static size fo a number: '{0}'", ex2.Message), ex2);
                                }
                            }


                            try
                            {
                                // rename files
                                currentOffset = fileNameOffset;

                                for (int i = 0; i < sourceFileCount; i++)
                                {
                                    sourceFile = sourceFiles[i];

                                    // get name for files with a terminator
                                    if (inputValues.FileNameHasTerminator)
                                    {
                                        destinationFileBytesSize = ParseFile.GetNextOffset(listFs, currentOffset, destinationFileTerminator) - currentOffset;
                                    }

                                    // read destination file name
                                    if (destinationFileBytesSize > 0)
                                    {
                                        destinationFileBytes = ParseFile.ParseSimpleOffset(listFs, currentOffset, (int)destinationFileBytesSize);
                                        destinationFile      = this.getFileNameFromEncodedBytes(inputValues.FileNameEncoding, inputValues.FileNameCodePage, destinationFileBytes);
                                        currentOffset       += destinationFileBytesSize + destinationFileTerminator.Length;
                                    }
                                    else
                                    {
                                        this.progressStruct.Clear();
                                        this.progressStruct.FileName       = sourceFile;
                                        this.progressStruct.GenericMessage = String.Format("Warning: End of List File reached when renaming <{0}>{1}", sourceFile, Environment.NewLine);
                                        this.ReportProgress(Constants.ProgressMessageOnly, progressStruct);
                                        break;
                                    }

                                    // rename file and build scripts
                                    destinationFile = Path.Combine(inputValues.SourceFolder, destinationFile).Trim();

                                    if (inputValues.KeepOriginalFileExtension)
                                    {
                                        if (String.IsNullOrEmpty(Path.GetExtension(destinationFile)))
                                        {
                                            destinationFile += Path.GetExtension(sourceFile);
                                        }
                                        else
                                        {
                                            Path.ChangeExtension(destinationFile, Path.GetExtension(sourceFile));
                                        }
                                    }

                                    if (sourceFile != destinationFile)
                                    {
                                        renameScript.AppendFormat("rename \"{0}\" \"{1}\" {2}", Path.GetFileName(sourceFile), Path.GetFileName(destinationFile), Environment.NewLine);
                                        undoScript.AppendFormat("rename \"{0}\" \"{1}\" {2}", Path.GetFileName(destinationFile), Path.GetFileName(sourceFile), Environment.NewLine);

                                        destinationFolder = Path.GetDirectoryName(destinationFile);

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

                                        File.Move(sourceFile, destinationFile);
                                    }



                                    // report progress
                                    this.progressStruct.Clear();
                                    this.progressStruct.FileName = sourceFile;
                                    this.ReportProgress(((i + 1) * 100) / sourceFileCount, progressStruct);
                                }
                            }
                            finally
                            {
                                if (renameScript.Length > 0)
                                {
                                    // write to file.
                                    FileUtil.CreateFileFromString(Path.Combine(inputValues.SourceFolder, RENAME_SCRIPT_NAME), renameScript.ToString());
                                }

                                if (undoScript.Length > 0)
                                {
                                    // write to file.
                                    FileUtil.CreateFileFromString(Path.Combine(inputValues.SourceFolder, UNDO_RENAME_SCRIPT_NAME), undoScript.ToString());
                                }
                            }
                        } // if (listFileLength < fileNameOffset)
                    }     // using (FileStream listFs = File.Open(inputValues.ListFileLocation, FileMode.Open, FileAccess.Read, FileShare.Read))
                }         // if (sourceFileCount == 0)
            }
            else
            {
                throw new IOException(String.Format("Directory not found: <{0}>", inputValues.SourceFolder));
            } // if (Directory.Exists(inputValues.SourceFolder))
        }
Esempio n. 3
0
        protected CriAfs2Archive InitializeExternalAwbArchive()
        {
            CriAfs2Archive afs2 = null;

            string awbDirectory;
            string awbMask;
            string acbBaseFileName;

            string[] awbFiles;

            byte[] awbHashStored;
            byte[] awbHashCalculated;

            awbDirectory = Path.GetDirectoryName(this.SourceFile);

            // try format 1
            acbBaseFileName = Path.GetFileNameWithoutExtension(this.SourceFile);
            awbMask         = String.Format(AWB_FORMAT1, acbBaseFileName);
            awbFiles        = Directory.GetFiles(awbDirectory, awbMask, SearchOption.TopDirectoryOnly);

            if (awbFiles.Length < 1)
            {
                // try format 2
                awbMask  = String.Format(AWB_FORMAT2, acbBaseFileName);
                awbFiles = Directory.GetFiles(awbDirectory, awbMask, SearchOption.TopDirectoryOnly);
            }

            if (awbFiles.Length < 1)
            {
                // try format 3
                awbMask  = String.Format(AWB_FORMAT3, acbBaseFileName);
                awbFiles = Directory.GetFiles(awbDirectory, awbMask, SearchOption.TopDirectoryOnly);
            }

            // file not found
            if (awbFiles.Length < 1)
            {
                throw new FileNotFoundException(String.Format("Cannot find AWB file. Please verify corresponding AWB file is named '{0}', '{1}', or '{2}'.",
                                                              String.Format(AWB_FORMAT1, acbBaseFileName), String.Format(AWB_FORMAT2, acbBaseFileName), String.Format(AWB_FORMAT3, acbBaseFileName)));
            }

            if (awbFiles.Length > 1)
            {
                throw new FileNotFoundException(String.Format("More than one matching AWB file for this ACB. Please verify only one AWB file is named '{1}' or '{2}'.",
                                                              String.Format(AWB_FORMAT1, acbBaseFileName), String.Format(AWB_FORMAT2, acbBaseFileName)));
            }

            // initialize AFS2 file
            using (FileStream fs = File.Open(awbFiles[0], FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                awbHashStored = this.StreamAwbHash;

                if (awbHashStored.Length == 0x10) // MD5, hash in newer format unknown
                {
                    // validate MD5 checksum
                    awbHashCalculated = ByteConversion.GetBytesFromHexString(ChecksumUtil.GetMd5OfFullFile(fs));

                    if (!ParseFile.CompareSegment(awbHashCalculated, 0, awbHashStored))
                    {
                        throw new FormatException(String.Format("AWB file, <{0}>, did not match checksum inside ACB file.", Path.GetFileName(fs.Name)));
                    }
                }

                afs2 = new CriAfs2Archive(fs, 0);
            }

            return(afs2);
        }