Exemple #1
0
        private void rebuildFile(string pFilePath, RebuildSetsStruct pRebuildSetsStruct, AuditingUtil pAuditingUtil,
                                 DoWorkEventArgs ea)
        {
            ArrayList pDestinationFiles     = new ArrayList();
            bool      isFileLibrary         = false;
            bool      hasMultipleExtensions = false;
            bool      fileMovedOrScannedOk  = true;
            Type      formatType            = null;

            if (FormatUtil.IsZipFile(pFilePath))
            {
                string tempDir = System.IO.Path.GetTempPath() + Path.GetFileNameWithoutExtension(pFilePath);
                CompressionUtil.ExtractAllFilesFromZipFile(pFilePath, tempDir);

                tempDirsForDeletion.Add(tempDir);           // Add in case of cancel button

                RebuildSetsStruct zipRebuildSetsStruct = pRebuildSetsStruct;
                zipRebuildSetsStruct.pSourceDir = tempDir;
                zipRebuildSetsStruct.totalFiles = Directory.GetFiles(tempDir, "*.*", SearchOption.AllDirectories).Length;

                maxFiles += zipRebuildSetsStruct.totalFiles;
                this.rebuildSets(zipRebuildSetsStruct, pAuditingUtil, 0, ea);

                if (!CancellationPending)
                {
                    Directory.Delete(tempDir, true);
                    tempDirsForDeletion.Remove(tempDir);
                }
            }
            else
            {
                using (FileStream fs = File.OpenRead(pFilePath))
                {
                    formatType = FormatUtil.getObjectType(fs);
                    fs.Seek(0, SeekOrigin.Begin);

                    // CRC32
                    string crc32Value     = String.Empty;
                    Crc32  crc32Generator = new Crc32();

                    // @TODO - Change to file streams?  Out of memory errors on repeat runs

                    /*
                     * // MD5
                     * string md5FileName = Path.GetTempFileName();
                     * MD5CryptoServiceProvider md5Hash = new MD5CryptoServiceProvider();
                     * //MemoryStream md5MemoryStream = new MemoryStream();
                     * FileStream md5MemoryStream = new FileStream(md5FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                     * CryptoStream md5CryptoStream = new CryptoStream(md5MemoryStream, md5Hash, CryptoStreamMode.Write);
                     *
                     * // SHA1
                     * string sha1FileName = Path.GetTempFileName();
                     * SHA1CryptoServiceProvider sha1Hash = new SHA1CryptoServiceProvider();
                     * //MemoryStream sha1MemoryStream = new MemoryStream();
                     * FileStream sha1MemoryStream = new FileStream(sha1FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                     * CryptoStream sha1CryptoStream = new CryptoStream(sha1MemoryStream, sha1Hash, CryptoStreamMode.Write);
                     */


                    if ((pAuditingUtil.HasVgmtSizeFlag) && (formatType != null))
                    {
                        try
                        {
                            IFormat vgmData = (IFormat)Activator.CreateInstance(formatType);
                            vgmData.Initialize(fs, pFilePath);

                            isFileLibrary         = vgmData.IsFileLibrary();
                            hasMultipleExtensions = vgmData.HasMultipleFileExtensions();
                            // vgmData.getDatFileCrc32(pFilePath, ref libHash, ref crc32Generator,
                            //    ref md5CryptoStream, ref sha1CryptoStream, false, pStreamInput);
                            vgmData.GetDatFileCrc32(ref crc32Generator);
                            vgmData = null;
                        }
                        catch (EndOfStreamException e)
                        {
                            this.progressStruct.Clear();
                            this.progressStruct.FileName     = pFilePath;
                            this.progressStruct.ErrorMessage = String.Format("Error processing <{0}> as type [{1}], falling back to full file cheksum.  Error received: {2}", pFilePath, formatType.Name, e.Message) + Environment.NewLine + Environment.NewLine;
                            ReportProgress(Constants.IgnoreProgress, this.progressStruct);

                            crc32Generator.Reset();
                            // ParseFile.AddChunkToChecksum(fs, 0, (int)fs.Length, ref crc32Generator,
                            //    ref md5CryptoStream, ref sha1CryptoStream);
                            ChecksumUtil.AddChunkToChecksum(fs, 0, (int)fs.Length, ref crc32Generator);
                        }
                        catch (System.OutOfMemoryException e)
                        {
                            this.progressStruct.Clear();
                            this.progressStruct.FileName     = pFilePath;
                            this.progressStruct.ErrorMessage = String.Format("Error processing <{0}> as type [{1}], falling back to full file cheksum.  Error received: {2}", pFilePath, formatType.Name, e.Message) + Environment.NewLine + Environment.NewLine;
                            ReportProgress(Constants.IgnoreProgress, this.progressStruct);

                            crc32Generator.Reset();
                            // ParseFile.AddChunkToChecksum(fs, 0, (int)fs.Length, ref crc32Generator,
                            //    ref md5CryptoStream, ref sha1CryptoStream);
                            ChecksumUtil.AddChunkToChecksum(fs, 0, (int)fs.Length, ref crc32Generator);
                        }
                        catch (IOException e)
                        {
                            this.progressStruct.Clear();
                            this.progressStruct.FileName     = pFilePath;
                            this.progressStruct.ErrorMessage = String.Format("Error processing <{0}> as type [{1}], falling back to full file cheksum.  Error received: {2}", pFilePath, formatType.Name, e.Message) + Environment.NewLine + Environment.NewLine;
                            ReportProgress(Constants.IgnoreProgress, this.progressStruct);

                            crc32Generator.Reset();
                            // ParseFile.AddChunkToChecksum(fs, 0, (int)fs.Length, ref crc32Generator,
                            //    ref md5CryptoStream, ref sha1CryptoStream);
                            ChecksumUtil.AddChunkToChecksum(fs, 0, (int)fs.Length, ref crc32Generator);
                        }
                    }
                    else
                    {
                        // ParseFile.AddChunkToChecksum(fs, 0, (int)fs.Length, ref crc32Generator,
                        //    ref md5CryptoStream, ref sha1CryptoStream);
                        ChecksumUtil.AddChunkToChecksum(fs, 0, (int)fs.Length, ref crc32Generator);
                    }

                    // @TODO Add MD5/SHA1 to make checksum hash correct String(CRC32 + MD5 + SHA1)
                    crc32Value = crc32Generator.Value.ToString("X8");

                    pDestinationFiles = (ArrayList)pAuditingUtil.ChecksumHash[crc32Value];
                    if (pDestinationFiles != null)
                    {
                        if (!pRebuildSetsStruct.ScanOnly)
                        {
                            fileMovedOrScannedOk = this.moveFile(pRebuildSetsStruct.pDestinationDir, pDestinationFiles, fs, pFilePath, pRebuildSetsStruct.pOverwriteExisting,
                                                                 pRebuildSetsStruct.pCompressOutput, hasMultipleExtensions);
                        }
                        pAuditingUtil.AddChecksumToCache(crc32Value);
                    }
                    else
                    {
                        pAuditingUtil.AddUnknownFile(pFilePath);
                    }
                }

                /*
                 * md5CryptoStream.Close();
                 * md5CryptoStream.Dispose();
                 * sha1CryptoStream.Close();
                 * sha1CryptoStream.Dispose();
                 *
                 * md5MemoryStream.Close();
                 * md5MemoryStream.Dispose();
                 * sha1MemoryStream.Close();
                 * sha1MemoryStream.Dispose();
                 *
                 * File.Delete(md5FileName);
                 * File.Delete(sha1FileName);
                 */

                // Remove Source only if copied
                if (fileMovedOrScannedOk && pDestinationFiles != null &&
                    pRebuildSetsStruct.pRemoveSource && File.Exists(pFilePath))
                {
                    if (!isFileLibrary)
                    {
                        File.Delete(pFilePath);
                    }
                    else // Add to List for deletion later
                    {
                        libFilesForDeletion.Add(pFilePath);
                    }
                }
            } // (FormatUtil.IsZipFile(pFilePath))
        }