/// <summary> /// Decompress the entry to a stream. /// </summary> /// <param name="entry">The entry which will be decompressed</param> /// <param name="outStream">The output stream</param> public void ExtractEntry(CMFEntry entry, Stream outStream) { if (this._disposed) { throw new System.ObjectDisposedException("Archive"); } if (!outStream.CanWrite) { throw new InvalidOperationException("The stream should be writable."); } long entrydataoffset = entry.dataoffset + this.dataoffsetStart; this.BaseStream.Seek(entrydataoffset, SeekOrigin.Begin); if (!CmfFormat.IsEncryptedFile(entry.FileName) && entry.IsCompressed) { using (Stream srcStream = new ZlibStream(new EntryStream(this.BaseStream, entrydataoffset, entry.CompressedSize, true), System.IO.Compression.CompressionMode.Decompress, false)) { srcStream.CopyTo(outStream); outStream.Flush(); } } else { // Let's extract encrypted content as raw data, too. // Because I don't know how to decrypt. using (Stream srcStream = new EntryStream(this.BaseStream, entrydataoffset, entry.UnpackedSize, true)) { srcStream.CopyTo(outStream); outStream.Flush(); } } }
public Stream OpenEntryStream() { if (this._disposed) { throw new System.ObjectDisposedException("Reader"); } long entrydataoffset = this.Entry.dataoffset + this.dataoffsetStart; this.sourceArchive.BaseStream.Seek(entrydataoffset, SeekOrigin.Begin); if (!CmfFormat.IsEncryptedFile(this.Entry.FileName) && this.Entry.IsCompressed) { this.currentStream = new ZlibStream(new EntryStream(this.sourceArchive.BaseStream, entrydataoffset, Entry.CompressedSize, true), System.IO.Compression.CompressionMode.Decompress, false); } else { // Let's extract encrypted content as raw data, too. // Because I don't know how to decrypt. this.currentStream = new EntryStream(this.sourceArchive.BaseStream, entrydataoffset, Entry.UnpackedSize, true); } return(this.currentStream); }
private bool SetEntryData(CMFEntry entry, object data, long length) { if (this._disposed) { throw new System.ObjectDisposedException("Editor"); } if (this._issaving) { throw new InvalidOperationException("The Editor is writing data. Adding more data is not possible."); // Although it is possible. But I'm lazy. } if (entry == null || this.myArchive.Entries.IndexOf(entry) == -1) { return(false); } else { if (length == -1) { if (data is Stream stream) { FileStream fs = SpawnTempFile(); if (!CmfFormat.IsEncryptedFile(entry.FileName) && entry.IsCompressed) { using (ZlibStream zlibstream = new ZlibStream(fs, System.IO.Compression.CompressionMode.Compress, this.CompressionLevel, true, Encoding.UTF8)) CopyStream(stream, zlibstream, ref this.myultimatebuffer); } else { CopyStream(stream, fs, ref this.myultimatebuffer); } if (fs.Length > entry.CompressedSize) { fs.Dispose(); throw new InvalidDataException($"The new data of '{entry.FileName}' size is bigger than the original one."); } else { if (this.datadictionary.ContainsKey(entry)) { this.datadictionary[entry].Dispose(); } this.datadictionary[entry] = fs; fs.Flush(); } } else if (data is byte[] dataArray) { if (!CmfFormat.IsEncryptedFile(entry.FileName) && entry.IsCompressed) { FileStream fs = SpawnTempFile(); using (ZlibStream zlibstream = new ZlibStream(fs, System.IO.Compression.CompressionMode.Compress, this.CompressionLevel, true, Encoding.UTF8)) zlibstream.Write(dataArray, 0, dataArray.Length); if (fs.Length > entry.CompressedSize) { fs.Dispose(); throw new InvalidDataException($"The new data of '{entry.FileName}' size is bigger than the original one."); } else { if (this.datadictionary.ContainsKey(entry)) { this.datadictionary[entry].Dispose(); } this.datadictionary[entry] = fs; fs.Flush(); } } else { if (dataArray.Length > entry.CompressedSize) { throw new InvalidDataException($"The new data of '{entry.FileName}' size is bigger than the original one."); } else { FileStream fs = SpawnTempFile(); fs.Write(dataArray, 0, dataArray.Length); if (this.datadictionary.ContainsKey(entry)) { this.datadictionary[entry].Dispose(); } this.datadictionary[entry] = fs; fs.Flush(); } } } } else { if (data is Stream stream) { FileStream fs = SpawnTempFile(); if (length > 0) { if (!CmfFormat.IsEncryptedFile(entry.FileName) && entry.IsCompressed) { using (ZlibStream zlibstream = new ZlibStream(fs, System.IO.Compression.CompressionMode.Compress, this.CompressionLevel, true, Encoding.UTF8)) CopyStream(stream, zlibstream, ref this.myultimatebuffer, length); } else { CopyStream(stream, fs, ref this.myultimatebuffer, length); } } if (fs.Length > entry.CompressedSize) { fs.Dispose(); throw new InvalidDataException($"The new data of '{entry.FileName}' size is bigger than the original one."); } else { if (this.datadictionary.ContainsKey(entry)) { this.datadictionary[entry].Dispose(); } this.datadictionary[entry] = fs; fs.Flush(); } } else if (data is byte[] dataArray) { if (!CmfFormat.IsEncryptedFile(entry.FileName) && entry.IsCompressed) { FileStream fs = SpawnTempFile(); using (ZlibStream zlibstream = new ZlibStream(fs, System.IO.Compression.CompressionMode.Compress, this.CompressionLevel, true, Encoding.UTF8)) zlibstream.Write(dataArray, 0, dataArray.Length); if (fs.Length > entry.CompressedSize) { fs.Dispose(); throw new InvalidDataException($"The new data of '{entry.FileName}' size is bigger than the original one."); } else { if (this.datadictionary.ContainsKey(entry)) { this.datadictionary[entry].Dispose(); } this.datadictionary[entry] = fs; fs.Flush(); } } else { if (dataArray.Length > entry.CompressedSize) { throw new InvalidDataException($"The new data of '{entry.FileName}' size is bigger than the original one."); } else { FileStream fs = SpawnTempFile(); fs.Write(dataArray, 0, dataArray.Length); if (this.datadictionary.ContainsKey(entry)) { this.datadictionary[entry].Dispose(); } this.datadictionary[entry] = fs; fs.Flush(); } } } } return(true); } }