private void TreatHeader(FileInArchive archFile) { MemoryStream memoryStream = new MemoryStream(); byte[] buffer = new byte[4096]; if ((int)archFile.descriptor.compressionMethod == 1) { try { Inflater inflater = new Inflater(); inflater.SetInput(archFile.data_start_200); inflater.Inflate(buffer); } catch (Exception ex) { this.Error_FileTableEntry(archFile); } archFile.descriptor.extension = this.GetExtension(buffer); } else { if ((int)archFile.descriptor.compressionMethod != 0) { return; } archFile.descriptor.extension = this.GetExtension(archFile.data_start_200); } }
private void ExtractFile(FileInArchive archFile) { this.error_ExtractionNumber = 0L; if (MypHandlerConfig.MultithreadedExtraction) { this.usedRam = this.getUsedRAM(); if ((double)this.usedRam > this.programMemory) { this.oldUsedRam = this.usedRam; ++this.garbageRuns; while (this.boList.Count > 100) { Thread.Sleep(1000); } GC.Collect(); this.usedRam = this.getUsedRAM(); } } archFile.data = new byte[archFile.descriptor.compressedSize]; this.archiveStream.Seek(archFile.descriptor.startingPosition + (long)archFile.descriptor.fileHeaderSize, SeekOrigin.Begin); for (int index = 0; index < archFile.data.Length; ++index) { archFile.data[index] = (byte)this.archiveStream.ReadByte(); } this.TreatExtractedFile(archFile); archFile.data = (byte[])null; }
/// <summary> /// Tries to identify the file extension /// Updates the file descriptor in response /// </summary> /// <param name="archFile">file to consider</param> private void TreatHeader(FileInArchive archFile) { MemoryStream outputMS = new MemoryStream(); byte[] output_buffer = new byte[4096]; if (archFile.descriptor.compressionMethod == 1) { try { ICSharpCode.SharpZipLib.Zip.Compression.Inflater inf = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater(); inf.SetInput(archFile.data_start_200); inf.Inflate(output_buffer); } catch (Exception) { Error_FileTableEntry(archFile); } archFile.descriptor.extension = GetExtension(output_buffer); //In order to sort the filenames in folders in the treeView. Yeah I cheat! //archFile.descriptor.filename = archFile.descriptor.extension + "/" + archFile.descriptor.filename; } else if (archFile.descriptor.compressionMethod == 0) { archFile.descriptor.extension = GetExtension(archFile.data_start_200); } else { } }
/// <summary> /// Tries to identify the file extension /// Updates the file descriptor in response /// </summary> /// <param name="archFile">file to consider</param> private void TreatHeader(FileInArchive archFile) { MemoryStream outputMS = new MemoryStream(); byte[] output_buffer = new byte[4096]; if (archFile.descriptor.compressionMethod == 1) { try { ICSharpCode.SharpZipLib.Zip.Compression.Inflater inf = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater(); inf.SetInput(archFile.data_start_200); inf.Inflate(output_buffer); } catch { Error_FileTableEntry(archFile); } archFile.descriptor.extension = GetExtension(output_buffer); } else if (archFile.descriptor.compressionMethod == 0) { archFile.descriptor.extension = GetExtension(archFile.data_start_200); } else { } }
/// <summary> /// Extracts a file, a list of file or search + extract a file in case of a string /// </summary> public void Extract(object obj) { boList = new BufferObjectList(); numExtractedFiles = 0; List <FileInArchive> fileList = new List <FileInArchive>(); if (obj.GetType() == typeof(List <FileInArchive>)) { fileList = (List <FileInArchive>)obj; } else if (obj.GetType() == typeof(FileInArchive)) { fileList.Add((FileInArchive)obj); } else if (obj.GetType() == typeof(string)) { FileInArchive tmpFIA = this.SearchForFile((string)obj); if (tmpFIA == null) { return; } fileList.Add(tmpFIA); } numOfFileInExtractionList = fileList.Count; //needed since the events are launched in the save buffer now //If we are using more than one disk, then we multi thread //Otherwise having 2 threads doing the reading and the writting is bad if (mypPath[0] == extractionPath[0]) { MypHandlerConfig.MultithreadedExtraction = false; } if (MypHandlerConfig.MultithreadedExtraction) { boList.Active = true; Thread t_BOwriter = new Thread(new ThreadStart(ThreadWrite)); t_BOwriter.Start(); //Multi threading the writes is useless even when we have lots of small files. //Thread t_BOwriter2 = new Thread(new ThreadStart(ThreadWrite)); //t_BOwriter2.Start(); } for (int i = 0; i < fileList.Count; i++) { ExtractFile(fileList[i]); } if (!MypHandlerConfig.MultithreadedExtraction) { TriggerExtractionEvent(new MYPFileEventArgs(Event_ExtractionType.ExtractionFinished, fileList.Count - error_ExtractionNumber)); } else { boList.Active = false; } }
/// <summary> /// Treats the data extracted from the myp archive /// Uncompress the data if said data was compressed /// </summary> /// <param name="archFile"></param> private void TreatExtractedFile(FileInArchive archFile) { try { if (archFile.descriptor.compressionMethod == 1) //ZLib compression { try { //Create the output_buffer, useless to create it if no compression byte[] output_buffer = new byte[archFile.descriptor.uncompressedSize]; ICSharpCode.SharpZipLib.Zip.Compression.Inflater inf = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater(); inf.SetInput(archFile.data); inf.Inflate(output_buffer); if (!MypHandlerConfig.MultithreadedExtraction) { //Treat directly the write SaveBufferToFile(output_buffer, archFile.descriptor.foundFileName, archFile.descriptor.filename, archFile.descriptor.extension); } else { boList.AddBufferItemToQueue(output_buffer, archFile.descriptor.foundFileName, archFile.descriptor.filename, archFile.descriptor.extension); } //Clear the buffer (useless in CSharp) output_buffer = null; } catch (Exception) { TriggerExtractionEvent(new MYPFileEventArgs(Event_ExtractionType.FileExtractionError, error_ExtractionNumber++)); } } else if (archFile.descriptor.compressionMethod == 0) //No compression { if (!MypHandlerConfig.MultithreadedExtraction) { //Treat directly the write SaveBufferToFile(archFile.data, archFile.descriptor.foundFileName, archFile.descriptor.filename, archFile.descriptor.extension); } else { boList.AddBufferItemToQueue(archFile.data, archFile.descriptor.foundFileName, archFile.descriptor.filename, archFile.descriptor.extension); } } else { TriggerExtractionEvent(new MYPFileEventArgs(Event_ExtractionType.UnknownCompressionMethod, error_ExtractionNumber++)); } } catch (Exception) { TriggerExtractionEvent(new MYPFileEventArgs(Event_ExtractionType.UnknownError, error_ExtractionNumber++)); } }
private void WriteFileToArchive(FileInArchive archFile, MemoryStream MS) { this.archiveStream.Close(); try { this.archiveStream = new FileStream(this.fullMypFileName, FileMode.Open, FileAccess.ReadWrite); } catch (Exception ex) { throw new Exception("You need to stop application currently using the following file: " + this.fullMypFileName); } this.archiveStream.Seek(archFile.descriptor.fileTableEntryPosition + 12L, SeekOrigin.Begin); this.archiveStream.Write(this.ConvertLongToByteArray((int)(MS.Length & (long)uint.MaxValue)), 0, 4); this.archiveStream.Seek(archFile.descriptor.fileTableEntryPosition + 16L, SeekOrigin.Begin); this.archiveStream.Write(this.ConvertLongToByteArray((int)archFile.descriptor.uncompressedSize & -1), 0, 4); this.archiveStream.Seek(archFile.descriptor.fileTableEntryPosition + 32L, SeekOrigin.Begin); this.archiveStream.Write(new byte[1] { archFile.CompressionMethod }, 0, 1); byte[] buffer1 = MS.GetBuffer(); if (MS.Length <= (long)archFile.descriptor.compressedSize) { this.archiveStream.Seek(archFile.descriptor.startingPosition + (long)archFile.descriptor.fileHeaderSize, SeekOrigin.Begin); this.archiveStream.Write(buffer1, 0, (int)MS.Length); } else { long length = this.archiveStream.Length; this.archiveStream.Seek(0L, SeekOrigin.End); if (archFile.metadata != null) { this.archiveStream.Write(archFile.metadata, 0, archFile.metadata.Length); } else { byte[] buffer2 = new byte[archFile.descriptor.fileHeaderSize]; this.archiveStream.Write(buffer2, 0, buffer2.Length); } this.archiveStream.Seek(0L, SeekOrigin.End); this.archiveStream.Write(buffer1, 0, (int)MS.Length); this.archiveStream.Seek(archFile.descriptor.fileTableEntryPosition, SeekOrigin.Begin); this.archiveStream.Write(this.ConvertLongToByteArray((int)(length & (long)uint.MaxValue)), 0, 4); this.archiveStream.Seek(archFile.descriptor.fileTableEntryPosition + 4L, SeekOrigin.Begin); this.archiveStream.Write(this.ConvertLongToByteArray((int)(length >> 32 & (long)uint.MaxValue)), 0, 4); archFile.descriptor.startingPosition = (long)(int)(length & (long)uint.MaxValue); } archFile.descriptor.compressedSize = (uint)MS.Length; this.archiveStream.Close(); this.archiveStream = new FileStream(this.fullMypFileName, FileMode.Open, FileAccess.Read); }
/// <summary> /// Extracts input file from the myp archive /// (this is temporary, it will soon be hidden so that the input parameter is a string) /// </summary> /// <param name="archFile">file to extract</param> public void ExtractFile(FileInArchive archFile) { error_ExtractionNumber = 0; archFile.data = new byte[archFile.descriptor.compressedSize]; archiveStream.Seek((long)(archFile.descriptor.startingPosition + archFile.descriptor.fileHeaderSize), SeekOrigin.Begin); for (int i = 0; i < archFile.data.Length; i++) { archFile.data[i] = (byte)archiveStream.ReadByte(); } TreatExtractedFile(archFile); archFile.data = null; }
private void TreatExtractedFile(FileInArchive archFile) { try { if ((int)archFile.descriptor.compressionMethod == 1) { try { byte[] buffer = new byte[archFile.descriptor.uncompressedSize]; Inflater inflater = new Inflater(); inflater.SetInput(archFile.data); inflater.Inflate(buffer); if (!MypHandlerConfig.MultithreadedExtraction) { this.SaveBufferToFile(buffer, archFile.descriptor.foundFileName, archFile.descriptor.filename, archFile.descriptor.extension); } else { this.boList.AddBufferItemToQueue(buffer, archFile.descriptor.foundFileName, archFile.descriptor.filename, archFile.descriptor.extension); } } catch (Exception ex) { this.TriggerExtractionEvent(new MYPFileEventArgs(Event_ExtractionType.FileExtractionError, this.error_ExtractionNumber++)); } } else if ((int)archFile.descriptor.compressionMethod == 0) { if (!MypHandlerConfig.MultithreadedExtraction) { this.SaveBufferToFile(archFile.data, archFile.descriptor.foundFileName, archFile.descriptor.filename, archFile.descriptor.extension); } else { this.boList.AddBufferItemToQueue(archFile.data, archFile.descriptor.foundFileName, archFile.descriptor.filename, archFile.descriptor.extension); } } else { this.TriggerExtractionEvent(new MYPFileEventArgs(Event_ExtractionType.UnknownCompressionMethod, this.error_ExtractionNumber++)); } } catch (Exception ex) { this.TriggerExtractionEvent(new MYPFileEventArgs(Event_ExtractionType.UnknownError, this.error_ExtractionNumber++)); } }
public void Extract(object obj) { this.boList = new BufferObjectList(); this.numExtractedFiles = 0L; List <FileInArchive> fileInArchiveList = new List <FileInArchive>(); if (obj.GetType() == typeof(List <FileInArchive>)) { fileInArchiveList = (List <FileInArchive>)obj; } else if (obj.GetType() == typeof(FileInArchive)) { fileInArchiveList.Add((FileInArchive)obj); } else if (obj.GetType() == typeof(string)) { FileInArchive fileInArchive = this.SearchForFile((string)obj); if (fileInArchive == null) { return; } fileInArchiveList.Add(fileInArchive); } this.numOfFileInExtractionList = fileInArchiveList.Count; if ((int)this.mypPath[0] == (int)this.extractionPath[0]) { MypHandlerConfig.MultithreadedExtraction = false; } if (MypHandlerConfig.MultithreadedExtraction) { this.boList.Active = true; new Thread(new ThreadStart(this.ThreadWrite)).Start(); } for (int index = 0; index < fileInArchiveList.Count; ++index) { this.ExtractFile(fileInArchiveList[index]); } if (!MypHandlerConfig.MultithreadedExtraction) { this.TriggerExtractionEvent(new MYPFileEventArgs(Event_ExtractionType.ExtractionFinished, (long)fileInArchiveList.Count - this.error_ExtractionNumber)); } else { this.boList.Active = false; } }
public void ReplaceFile(FileInArchive archFile, FileStream newFile) { byte[] buffer = new byte[newFile.Length]; newFile.Read(buffer, 0, buffer.Length); MemoryStream memoryStream = new MemoryStream(buffer); MemoryStream MS = new MemoryStream(); byte[] numArray = new byte[0]; int compressionMethod1 = (int)archFile.descriptor.compressionMethod; bool flag = true; int compressionMethod2 = (int)archFile.descriptor.compressionMethod; flag = false; archFile.descriptor.compressionMethod = (byte)0; this.CopyStream((Stream)memoryStream, (Stream)MS); archFile.descriptor.uncompressedSize = (uint)memoryStream.Length; this.WriteFileToArchive(archFile, MS); }
/// <summary> /// Treats the data extracted from the myp archive /// Uncompress the data if said data was compressed /// </summary> /// <param name="archFile"></param> private void TreatExtractedFile(FileInArchive archFile) { MemoryStream inputMS = new MemoryStream(archFile.data, 0, archFile.data.Length); MemoryStream outputMS = new MemoryStream(); byte[] output_buffer = new byte[archFile.descriptor.uncompressedSize]; try { if (archFile.descriptor.compressionMethod == 1) //ZLib compression { try { ICSharpCode.SharpZipLib.Zip.Compression.Inflater inf = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater(); inf.SetInput(archFile.data); inf.Inflate(output_buffer); SaveBufferToFile(output_buffer, archFile.descriptor.foundFileName, archFile.descriptor.filename, archFile.descriptor.extension); TriggerExtractionEvent(new MYPFileEventArgs(Event_ExtractionType.FileExtracted, numExtractedFiles++)); } catch { TriggerExtractionEvent(new MYPFileEventArgs(Event_ExtractionType.FileExtractionError, error_ExtractionNumber++)); } } else if (archFile.descriptor.compressionMethod == 0) //No compression { SaveBufferToFile(archFile.data, archFile.descriptor.foundFileName, archFile.descriptor.filename, archFile.descriptor.extension); TriggerExtractionEvent(new MYPFileEventArgs(Event_ExtractionType.FileExtracted, numExtractedFiles++)); } else { TriggerExtractionEvent(new MYPFileEventArgs(Event_ExtractionType.UnknownCompressionMethod, error_ExtractionNumber++)); } } catch { TriggerExtractionEvent(new MYPFileEventArgs(Event_ExtractionType.UnknownError, error_ExtractionNumber++)); } }
/// <summary> /// Replaces a file by a file /// </summary> /// <param name="archFile">file to replace</param> /// <param name="newFile">new file</param> public void ReplaceFile(FileInArchive archFile, FileStream newFile) { byte[] newFileBuffer = new byte[newFile.Length]; newFile.Read(newFileBuffer, 0, newFileBuffer.Length); MemoryStream inputMS = new MemoryStream(newFileBuffer); MemoryStream outputMS = new MemoryStream(); byte[] output_buffer = new byte[0]; if (archFile.descriptor.compressionMethod == 1 && false) //ZLib compression { try { ICSharpCode.SharpZipLib.Zip.Compression.Deflater def = new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(); ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream defstream = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream(outputMS, def); defstream.Write(newFileBuffer, 0, newFileBuffer.Length); defstream.Flush(); defstream.Finish(); output_buffer = outputMS.GetBuffer(); } catch { } archFile.descriptor.uncompressedSize = (uint)inputMS.Length; WriteFileToArchive(archFile, outputMS); } else if (archFile.descriptor.compressionMethod == 0 || true) //No compression { archFile.descriptor.compressionMethod = 0; CopyStream(inputMS, outputMS); archFile.descriptor.uncompressedSize = (uint)inputMS.Length; WriteFileToArchive(archFile, outputMS); } }
/// <summary> /// Extra selected files from the archive /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void extractSelectedToolStripMenuItem_Click(object sender, EventArgs e) { //Retrieve the selected file list SortedList <string, List <FileInArchive> > multiFileList = new SortedList <string, List <FileInArchive> >(); foreach (DataGridViewRow theRow in fileInArchiveDataGridView.SelectedRows) { if (theRow.DataBoundItem != null) { MYPHandler.FileInArchive fiaFile = (MYPHandler.FileInArchive)theRow.DataBoundItem; if (!multiFileList.Keys.Contains(fiaFile.sourceFileName)) { multiFileList.Add(fiaFile.sourceFileName, new List <FileInArchive>()); } multiFileList[fiaFile.sourceFileName].Add(fiaFile); } } Thread t_multiExtract = new Thread(new ParameterizedThreadStart(MultiListExtraction)); t_multiExtract.Start(multiFileList); }
/// <summary> /// Extracts input file from the myp archive /// </summary> /// <param name="archFile">file to extract</param> private void ExtractFile(FileInArchive archFile) { error_ExtractionNumber = 0; #region MultiThreading Stuff if (MypHandlerConfig.MultithreadedExtraction) { //Some stuff to free the memory //This is in case we explode the memory usedRam = getUsedRAM(); if (usedRam > programMemory) { oldUsedRam = usedRam; garbageRuns++; //We empty the file list until we get under 1000 //Redundant with the check done when adding a file while (boList.Count > 100) { Thread.Sleep(1000); } GC.Collect(); usedRam = getUsedRAM(); } } #endregion archFile.data = new byte[archFile.descriptor.compressedSize]; archiveStream.Seek((long)(archFile.descriptor.startingPosition + archFile.descriptor.fileHeaderSize), SeekOrigin.Begin); for (int i = 0; i < archFile.data.Length; i++) { archFile.data[i] = (byte)archiveStream.ReadByte(); } TreatExtractedFile(archFile); archFile.data = null; //nullify it because it has either been treated or copied }
/// <summary> /// Updates the number of errors in the file table entry /// Raise the according event /// </summary> /// <param name="archFile">File that gave the error</param> private void Error_FileTableEntry(FileInArchive archFile) { error_FileEntryNumber++; TriggerFileTableEvent(new MYPFileTableEventArgs(Event_FileTableType.FileError, archFile)); }
public MYPFileTableEventArgs(Event_FileTableType type, FileInArchive archFile) { this.type = type; this.archFile = archFile; }
/// <summary> /// Replaces a file by a file /// </summary> /// <param name="archFile">file to replace</param> /// <param name="newFile">new file</param> public void ReplaceFile(FileInArchive archFile, FileStream newFile) { byte[] newFileBuffer = new byte[newFile.Length]; newFile.Read(newFileBuffer, 0, newFileBuffer.Length); MemoryStream inputMS = new MemoryStream(newFileBuffer); MemoryStream outputMS = new MemoryStream(); byte[] output_buffer = new byte[0]; if (archFile.descriptor.compressionMethod == 1 && false) //ZLib compression { try { ICSharpCode.SharpZipLib.Zip.Compression.Deflater def = new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(); ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream defstream = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream(outputMS, def); defstream.Write(newFileBuffer, 0, newFileBuffer.Length); defstream.Flush(); defstream.Finish(); output_buffer = outputMS.GetBuffer(); } catch (Exception) { } archFile.descriptor.uncompressedSize = (uint)inputMS.Length; WriteFileToArchive(archFile, outputMS); } else if (archFile.descriptor.compressionMethod == 0 || true) //No compression { archFile.descriptor.compressionMethod = 0; CopyStream(inputMS, outputMS); archFile.descriptor.uncompressedSize = (uint)inputMS.Length; WriteFileToArchive(archFile, outputMS); } }
/// <summary> /// Parse the source file and reads the file table entries /// Raises according event depending on result /// </summary> public void GetFileTable() { error_FileEntryNumber = 0; error_ExtractionNumber = 0; unCompressedSize = 0; numberOfFileNamesFound = 0; numberOfFilesFound = 0; //Init long currentReadingPosition; uint numberOfFileInTable = 0; long endOfTableAddress; byte[] bufferTableHeader = new byte[12]; byte[] bufferFileDesc = new byte[FileInArchiveDescriptor.fileDescriptorSize]; FileInArchive myArchFile; while (tableStart != 0) { archiveStream.Seek(tableStart, SeekOrigin.Begin); archiveStream.Read(bufferTableHeader, 0, bufferTableHeader.Length); numberOfFileInTable = FileInArchiveDescriptor.convertLittleEndianBufferToInt(bufferTableHeader, 0); //get number of files currentReadingPosition = tableStart + 12; endOfTableAddress = tableStart + 12 + (long)FileInArchiveDescriptor.fileDescriptorSize * (long)numberOfFileInTable; // calculates the end address tableStart = FileInArchiveDescriptor.convertLittleEndianBufferToInt(bufferTableHeader, 4); //find the next filetable tableStart += (long)FileInArchiveDescriptor.convertLittleEndianBufferToInt(bufferTableHeader, 8) << 32; //mostly 0 #region File Table Read while (currentReadingPosition < endOfTableAddress) { archiveStream.Seek(currentReadingPosition, SeekOrigin.Begin); archiveStream.Read(bufferFileDesc, 0, bufferFileDesc.Length); myArchFile = new FileInArchive(); myArchFile.descriptor = new FileInArchiveDescriptor(bufferFileDesc); myArchFile.sourceFileName = fullMypFileName; myArchFile.descriptor.fileTableEntryPosition = currentReadingPosition; if (myArchFile.descriptor.startingPosition > 0 && myArchFile.descriptor.compressedSize > 0 && myArchFile.descriptor.uncompressedSize > 0 //If the compressed size is 0, then there is no file ) { //Search for the filename HashData ffn = null; if (hashDictionary != null) { ffn = hashDictionary.SearchHashList(myArchFile.descriptor.ph, myArchFile.descriptor.sh, currentMypFileName); if (ffn == null) // If we could not find the associated filename in the current archive, we look in the other archives hashes { // ffn = hashDictionary.SearchOtherArchive(myArchFile.descriptor.ph, myArchFile.descriptor.sh, currentMypFileName); } if (ffn != null && ffn.filename != "") { myArchFile.descriptor.foundFileName = true; myArchFile.descriptor.filename = ffn.filename; if (myArchFile.descriptor.crc != ffn.crc) { myArchFile.State = FileInArchiveState.MODIFIED; hashDictionary.UpdateCRC(myArchFile.descriptor.ph, myArchFile.descriptor.sh, myArchFile.descriptor.crc, currentMypFileName); archiveModifiedFileList.Add(myArchFile); } numberOfFileNamesFound++; int dotpos = myArchFile.descriptor.filename.LastIndexOf('.'); myArchFile.descriptor.extension = myArchFile.descriptor.filename.Substring(dotpos + 1); } else { if (ffn == null) { myArchFile.State = FileInArchiveState.NEW; hashDictionary.AddHash(myArchFile.descriptor.ph, myArchFile.descriptor.sh, "", myArchFile.descriptor.crc, currentMypFileName); archiveNewFileList.Add(myArchFile); } else if (myArchFile.descriptor.crc != ffn.crc) { myArchFile.State = FileInArchiveState.MODIFIED; hashDictionary.UpdateCRC(myArchFile.descriptor.ph, myArchFile.descriptor.sh, myArchFile.descriptor.crc, currentMypFileName); archiveModifiedFileList.Add(myArchFile); } //Retrieve header myArchFile.metadata = new byte[myArchFile.descriptor.fileHeaderSize]; archiveStream.Seek(myArchFile.descriptor.startingPosition, SeekOrigin.Begin); archiveStream.Read(myArchFile.metadata, 0, myArchFile.metadata.Length); //Retrieve head of the data for extension definition purpose if (!myArchFile.descriptor.foundFileName) { if (myArchFile.descriptor.compressedSize < myArchFile.data_start_200.Length) myArchFile.data_start_200 = new byte[myArchFile.descriptor.compressedSize]; archiveStream.Seek(myArchFile.descriptor.startingPosition + myArchFile.descriptor.fileHeaderSize, SeekOrigin.Begin); archiveStream.Read(myArchFile.data_start_200, 0, myArchFile.data_start_200.Length); try { TreatHeader(myArchFile); } catch (Exception) { Error_FileTableEntry(myArchFile); } } } //weird Wildcard match. //Updates the table try { numberOfFilesFound++; if (WildcardMatch(pattern, myArchFile.Filename)) { archiveFileList.Add(myArchFile); unCompressedSize += myArchFile.descriptor.uncompressedSize; /// \todo Speed: we should do something here to avoid passing an event for each and every file.... TriggerFileTableEvent(new MYPFileTableEventArgs(Event_FileTableType.NewFile, myArchFile)); } else { TriggerFileTableEvent(new MYPFileTableEventArgs(Event_FileTableType.UpdateFile, null)); } } catch (Exception) { Error_FileTableEntry(myArchFile); } } } else { //FileTableEntryError(myArchFile); } currentReadingPosition += FileInArchiveDescriptor.fileDescriptorSize; } // currentReadingPosition < endOfTableAddress #endregion } //tableStart != 0 TriggerFileTableEvent(new MYPFileTableEventArgs(Event_FileTableType.Finished, null)); }
/// <summary> /// Parse the source file and reads the file table entries /// Raises according event depending on result /// </summary> public void GetFileTable() { error_FileEntryNumber = 0; error_ExtractionNumber = 0; unCompressedSize = 0; numberOfFileNamesFound = 0; numberOfFilesFound = 0; //Init long currentReadingPosition; uint numberOfFileInTable = 0; long endOfTableAddress; byte[] bufferTableHeader = new byte[12]; byte[] bufferFileDesc = new byte[FileInArchiveDescriptor.fileDescriptorSize]; FileInArchive myArchFile; while (tableStart != 0) { archiveStream.Seek(tableStart, SeekOrigin.Begin); archiveStream.Read(bufferTableHeader, 0, bufferTableHeader.Length); numberOfFileInTable = FileInArchiveDescriptor.convertLittleEndianBufferToInt(bufferTableHeader, 0); //get number of files currentReadingPosition = tableStart + 12; endOfTableAddress = tableStart + 12 + (long)FileInArchiveDescriptor.fileDescriptorSize * (long)numberOfFileInTable; // calculates the end address tableStart = FileInArchiveDescriptor.convertLittleEndianBufferToInt(bufferTableHeader, 4); //find the next filetable tableStart += (long)FileInArchiveDescriptor.convertLittleEndianBufferToInt(bufferTableHeader, 8) << 32; //mostly 0 #region File Table Read while (currentReadingPosition < endOfTableAddress) { archiveStream.Seek(currentReadingPosition, SeekOrigin.Begin); archiveStream.Read(bufferFileDesc, 0, bufferFileDesc.Length); myArchFile = new FileInArchive(); myArchFile.descriptor = new FileInArchiveDescriptor(bufferFileDesc); myArchFile.descriptor.fileTableEntryPosition = currentReadingPosition; if (myArchFile.descriptor.startingPosition > 0 && myArchFile.descriptor.compressedSize > 0 && myArchFile.descriptor.uncompressedSize > 0 //If the compressed size is 0, then there is no file ) { //Search for the filename HashData ffn = null; if (hashDictionary != null) { ffn = hashDictionary.SearchHashList(myArchFile.descriptor.ph, myArchFile.descriptor.sh); if (ffn != null && ffn.filename != "") { myArchFile.descriptor.foundFileName = true; myArchFile.descriptor.filename = ffn.filename; if (myArchFile.descriptor.crc != ffn.crc) { myArchFile.State = FileInArchiveState.MODIFIED; hashDictionary.UpdateCRC(myArchFile.descriptor.ph, myArchFile.descriptor.sh, myArchFile.descriptor.crc); archiveModifiedFileList.Add(myArchFile); } numberOfFileNamesFound++; int dotpos = myArchFile.descriptor.filename.LastIndexOf('.'); myArchFile.descriptor.extension = myArchFile.descriptor.filename.Substring(dotpos + 1); } else { if (ffn == null) { myArchFile.State = FileInArchiveState.NEW; hashDictionary.AddHash(myArchFile.descriptor.ph, myArchFile.descriptor.sh, "", myArchFile.descriptor.crc); archiveNewFileList.Add(myArchFile); } else if (myArchFile.descriptor.crc != ffn.crc) { myArchFile.State = FileInArchiveState.MODIFIED; hashDictionary.UpdateCRC(myArchFile.descriptor.ph, myArchFile.descriptor.sh, myArchFile.descriptor.crc); archiveModifiedFileList.Add(myArchFile); } //Retrieve header myArchFile.metadata = new byte[myArchFile.descriptor.fileHeaderSize]; archiveStream.Seek(myArchFile.descriptor.startingPosition, SeekOrigin.Begin); archiveStream.Read(myArchFile.metadata, 0, myArchFile.metadata.Length); //Retrieve head of the data for extension definition purpose if (!myArchFile.descriptor.foundFileName) { if (myArchFile.descriptor.compressedSize < myArchFile.data_start_200.Length) { myArchFile.data_start_200 = new byte[myArchFile.descriptor.compressedSize]; } archiveStream.Seek(myArchFile.descriptor.startingPosition + myArchFile.descriptor.fileHeaderSize, SeekOrigin.Begin); archiveStream.Read(myArchFile.data_start_200, 0, myArchFile.data_start_200.Length); try { TreatHeader(myArchFile); } catch { Error_FileTableEntry(myArchFile); } } } //weird Wildcard match. //Updates the table try { numberOfFilesFound++; if (WildcardMatch(pattern, myArchFile.Filename)) { archiveFileList.Add(myArchFile); unCompressedSize += myArchFile.descriptor.uncompressedSize; /// \todo Speed: we should do something here to avoid passing an event for each and every file.... TriggerFileTableEvent(new MYPFileTableEventArgs(Event_FileTableType.NewFile, myArchFile)); } else { TriggerFileTableEvent(new MYPFileTableEventArgs(Event_FileTableType.UpdateFile, null)); } } catch { Error_FileTableEntry(myArchFile); } } } else { //FileTableEntryError(myArchFile); } currentReadingPosition += FileInArchiveDescriptor.fileDescriptorSize; } // currentReadingPosition < endOfTableAddress #endregion } //tableStart != 0 TriggerFileTableEvent(new MYPFileTableEventArgs(Event_FileTableType.Finished, null)); }
/// <summary> /// Writes buffer to the file and do some other treatment as well /// </summary> /// <param name="archFile">file to write</param> /// <param name="MS">memory stream containing the data</param> private void WriteFileToArchive(FileInArchive archFile, MemoryStream MS) { archiveStream.Close(); try { archiveStream = new FileStream(fullMypFileName, FileMode.Open, FileAccess.ReadWrite); } catch (Exception) { throw new Exception("You need to stop application currently using the following file: " + fullMypFileName); } #region Writting archiveStream.Seek((long)archFile.descriptor.fileTableEntryPosition + 12, SeekOrigin.Begin); int lowMSLength; lowMSLength = (int)(MS.Length & 0xFFFFFFFF); archiveStream.Write(ConvertLongToByteArray(lowMSLength), 0, 4); archiveStream.Seek((long)archFile.descriptor.fileTableEntryPosition + 16, SeekOrigin.Begin); lowMSLength = (int)(archFile.descriptor.uncompressedSize & 0xFFFFFFFF); archiveStream.Write(ConvertLongToByteArray(lowMSLength), 0, 4); archiveStream.Seek((long)archFile.descriptor.fileTableEntryPosition + 32, SeekOrigin.Begin); byte[] bArray = new byte[1]; bArray[0] = (byte)archFile.CompressionMethod; archiveStream.Write(bArray, 0, 1); byte[] tmp_bytearray = MS.GetBuffer(); if (MS.Length <= archFile.descriptor.compressedSize) { archiveStream.Seek((long)(archFile.descriptor.startingPosition + archFile.descriptor.fileHeaderSize), SeekOrigin.Begin); archiveStream.Write(tmp_bytearray, 0, (int)MS.Length); } else { long fileSize = archiveStream.Length; archiveStream.Seek(0, SeekOrigin.End); if (archFile.metadata != null) { archiveStream.Write(archFile.metadata, 0, archFile.metadata.Length); } else { byte[] fakeMetadata = new byte[archFile.descriptor.fileHeaderSize]; archiveStream.Write(fakeMetadata, 0, fakeMetadata.Length); //Should throw an exception, but not all files have meta data so... } archiveStream.Seek(0, SeekOrigin.End); archiveStream.Write(tmp_bytearray, 0, (int)MS.Length); archiveStream.Seek((long)archFile.descriptor.fileTableEntryPosition, SeekOrigin.Begin); archiveStream.Write(ConvertLongToByteArray((int)(fileSize & 0xFFFFFFFF)), 0, 4); archiveStream.Seek((long)archFile.descriptor.fileTableEntryPosition + 4, SeekOrigin.Begin); archiveStream.Write(ConvertLongToByteArray((int)((fileSize >> 32) & 0xFFFFFFFF)), 0, 4); archFile.descriptor.startingPosition = (int)(fileSize & 0xFFFFFFFF); } archFile.descriptor.compressedSize = (uint)MS.Length; tmp_bytearray = null; #endregion archiveStream.Close(); archiveStream = new FileStream(fullMypFileName, FileMode.Open, FileAccess.Read); }
private void FileListing_Add(FileInArchive file) { fileInArchiveBindingSource.Add(file); }
/// <summary> /// Writes buffer to the file and do some other treatment as well /// </summary> /// <param name="archFile">file to write</param> /// <param name="MS">memory stream containing the data</param> private void WriteFileToArchive(FileInArchive archFile, MemoryStream MS) { archiveStream.Close(); try { archiveStream = new FileStream(fullMypFileName, FileMode.Open, FileAccess.ReadWrite); } catch { throw new Exception("You need to stop application currently using the following file: " + fullMypFileName); } #region Writting archiveStream.Seek((long)archFile.descriptor.fileTableEntryPosition + 12, SeekOrigin.Begin); int lowMSLength; lowMSLength = (int)(MS.Length & 0xFFFFFFFF); archiveStream.Write(ConvertLongToByteArray(lowMSLength), 0, 4); archiveStream.Seek((long)archFile.descriptor.fileTableEntryPosition + 16, SeekOrigin.Begin); lowMSLength = (int)(archFile.descriptor.uncompressedSize & 0xFFFFFFFF); archiveStream.Write(ConvertLongToByteArray(lowMSLength), 0, 4); archiveStream.Seek((long)archFile.descriptor.fileTableEntryPosition + 32, SeekOrigin.Begin); byte[] bArray = new byte[1]; bArray[0] = (byte)archFile.CompressionMethod; archiveStream.Write(bArray, 0, 1); byte[] tmp_bytearray = MS.GetBuffer(); if (MS.Length <= archFile.descriptor.compressedSize) { archiveStream.Seek((long)(archFile.descriptor.startingPosition + archFile.descriptor.fileHeaderSize), SeekOrigin.Begin); archiveStream.Write(tmp_bytearray, 0, (int)MS.Length); } else { long fileSize = archiveStream.Length; archiveStream.Seek(0, SeekOrigin.End); if (archFile.metadata != null) { archiveStream.Write(archFile.metadata, 0, archFile.metadata.Length); } else { byte[] fakeMetadata = new byte[archFile.descriptor.fileHeaderSize]; archiveStream.Write(fakeMetadata, 0, fakeMetadata.Length); //Should throw an exception, but not all files have meta data so... } archiveStream.Seek(0, SeekOrigin.End); archiveStream.Write(tmp_bytearray, 0, (int)MS.Length); archiveStream.Seek((long)archFile.descriptor.fileTableEntryPosition, SeekOrigin.Begin); archiveStream.Write(ConvertLongToByteArray((int)(fileSize & 0xFFFFFFFF)), 0, 4); archiveStream.Seek((long)archFile.descriptor.fileTableEntryPosition + 4, SeekOrigin.Begin); archiveStream.Write(ConvertLongToByteArray((int)((fileSize >> 32) & 0xFFFFFFFF)), 0, 4); archFile.descriptor.startingPosition = (int)(fileSize & 0xFFFFFFFF); } archFile.descriptor.compressedSize = (uint)MS.Length; tmp_bytearray = null; #endregion archiveStream.Close(); archiveStream = new FileStream(fullMypFileName, FileMode.Open, FileAccess.Read); }
public void GetFileTable() { this.error_FileEntryNumber = 0L; this.error_ExtractionNumber = 0L; this.unCompressedSize = 0L; this.numberOfFileNamesFound = 0L; this.numberOfFilesFound = 0L; byte[] numArray = new byte[12]; byte[] buffer = new byte[FileInArchiveDescriptor.fileDescriptorSize]; while (this.tableStart != 0L) { this.archiveStream.Seek(this.tableStart, SeekOrigin.Begin); this.archiveStream.Read(numArray, 0, numArray.Length); uint num1 = FileInArchiveDescriptor.convertLittleEndianBufferToInt(numArray, 0L); long offset = this.tableStart + 12L; long num2 = this.tableStart + 12L + (long)FileInArchiveDescriptor.fileDescriptorSize * (long)num1; this.tableStart = (long)FileInArchiveDescriptor.convertLittleEndianBufferToInt(numArray, 4L); this.tableStart += (long)FileInArchiveDescriptor.convertLittleEndianBufferToInt(numArray, 8L) << 32; while (offset < num2) { this.archiveStream.Seek(offset, SeekOrigin.Begin); this.archiveStream.Read(buffer, 0, buffer.Length); FileInArchive archFile = new FileInArchive(); archFile.descriptor = new FileInArchiveDescriptor(buffer); archFile.sourceFileName = this.fullMypFileName; archFile.descriptor.fileTableEntryPosition = offset; if (archFile.descriptor.startingPosition > 0L && archFile.descriptor.compressedSize > 0U && archFile.descriptor.uncompressedSize > 0U) { if (this.hashDictionary != null) { HashData hashData = this.hashDictionary.SearchHashList(archFile.descriptor.ph, archFile.descriptor.sh); if (hashData != null && (string)hashData.filename != "") { archFile.descriptor.foundFileName = true; archFile.descriptor.filename = (string)hashData.filename; if (archFile.descriptor.crc != hashData.crc) { archFile.State = FileInArchiveState.MODIFIED; this.hashDictionary.UpdateCRC(archFile.descriptor.ph, archFile.descriptor.sh, archFile.descriptor.crc); this.archiveModifiedFileList.Add(archFile); } ++this.numberOfFileNamesFound; int num3 = archFile.descriptor.filename.LastIndexOf('.'); archFile.descriptor.extension = archFile.descriptor.filename.Substring(num3 + 1); } else { if (hashData == null) { archFile.State = FileInArchiveState.NEW; this.hashDictionary.AddHash(archFile.descriptor.ph, archFile.descriptor.sh, "", archFile.descriptor.crc); this.archiveNewFileList.Add(archFile); } else if (archFile.descriptor.crc != hashData.crc) { archFile.State = FileInArchiveState.MODIFIED; this.hashDictionary.UpdateCRC(archFile.descriptor.ph, archFile.descriptor.sh, archFile.descriptor.crc); this.archiveModifiedFileList.Add(archFile); } archFile.metadata = new byte[archFile.descriptor.fileHeaderSize]; this.archiveStream.Seek(archFile.descriptor.startingPosition, SeekOrigin.Begin); this.archiveStream.Read(archFile.metadata, 0, archFile.metadata.Length); if (!archFile.descriptor.foundFileName) { if ((long)archFile.descriptor.compressedSize < (long)archFile.data_start_200.Length) { archFile.data_start_200 = new byte[archFile.descriptor.compressedSize]; } this.archiveStream.Seek(archFile.descriptor.startingPosition + (long)archFile.descriptor.fileHeaderSize, SeekOrigin.Begin); this.archiveStream.Read(archFile.data_start_200, 0, archFile.data_start_200.Length); try { this.TreatHeader(archFile); } catch (Exception ex) { this.Error_FileTableEntry(archFile); } } } try { ++this.numberOfFilesFound; if (this.WildcardMatch(this.pattern, archFile.Filename)) { this.archiveFileList.Add(archFile); this.unCompressedSize += (long)archFile.descriptor.uncompressedSize; this.TriggerFileTableEvent(new MYPFileTableEventArgs(Event_FileTableType.NewFile, archFile)); } else { this.TriggerFileTableEvent(new MYPFileTableEventArgs(Event_FileTableType.UpdateFile, (FileInArchive)null)); } } catch (Exception ex) { this.Error_FileTableEntry(archFile); } } } offset += (long)FileInArchiveDescriptor.fileDescriptorSize; } } this.TriggerFileTableEvent(new MYPFileTableEventArgs(Event_FileTableType.Finished, (FileInArchive)null)); }