/// <summary> /// Complete cleanup as the final part of closing. /// </summary> /// <param name="testCrc">True if the crc value should be tested</param> void CompleteCloseEntry(bool testCrc) { StopDecrypting(); if ((flags & 8) != 0) { ReadDataDescriptor(); } size = 0; if (testCrc && ((crc.Value & 0xFFFFFFFFL) != entry.Crc) && (entry.Crc != -1)) { throw new BlubbZipException("CRC mismatch"); } crc.Reset(); if (method == (int)CompressionMethod.Deflated) { inf.Reset(); } entry = null; }
/// <summary> /// Write a data descriptor. /// </summary> /// <param name="entry">The entry to write a descriptor for.</param> /// <returns>Returns the number of descriptor bytes written.</returns> public int WriteDataDescriptor(BlubbZipEntry entry) { if (entry == null) { throw new ArgumentNullException("entry"); } int result = 0; // Add data descriptor if flagged as required if ((entry.Flags & (int)GeneralBitFlags.Descriptor) != 0) { // The signature is not PKZIP originally but is now described as optional // in the PKZIP Appnote documenting trhe format. WriteLEInt(BlubbZipConstants.DataDescriptorSignature); WriteLEInt(unchecked ((int)(entry.Crc))); result += 8; if (entry.LocalHeaderRequiresBlubb64) { WriteLELong(entry.CompressedSize); WriteLELong(entry.Size); result += 16; } else { WriteLEInt((int)entry.CompressedSize); WriteLEInt((int)entry.Size); result += 8; } } return(result); }
public BlubbZipEntry(BlubbZipEntry entry) { if (entry == null) { throw new ArgumentNullException("entry"); } known = entry.known; name = entry.name; size = entry.size; compressedSize = entry.compressedSize; crc = entry.crc; dosTime = entry.dosTime; method = entry.method; comment = entry.comment; versionToExtract = entry.versionToExtract; versionMadeBy = entry.versionMadeBy; externalFileAttributes = entry.externalFileAttributes; flags = entry.flags; blubbFileIndex = entry.blubbFileIndex; offset = entry.offset; forceBlubb64_ = entry.forceBlubb64_; if (entry.extra != null) { extra = new byte[entry.extra.Length]; Array.Copy(entry.extra, 0, extra, 0, entry.extra.Length); } }
/// <summary> /// Closes the blubb input stream /// </summary> public override void Close() { internalReader = new ReadDataHandler(ReadingNotAvailable); crc = null; entry = null; base.Close(); }
/// <summary> /// Extract the contents of a blubb file. /// </summary> /// <param name="blubbFileName">The blubb file to extract from.</param> /// <param name="targetDirectory">The directory to save extracted information in.</param> /// <param name="overwrite">The style of <see cref="Overwrite">overwriting</see> to apply.</param> /// <param name="confirmDelegate">A delegate to invoke when confirming overwriting.</param> /// <param name="fileFilter">A filter to apply to files.</param> /// <param name="directoryFilter">A filter to apply to directories.</param> /// <param name="restoreDateTime">Flag indicating wether to restore the date and time for extracted files.</param> public void ExtractBlubb(string blubbFileName, string targetDirectory, Overwrite overwrite, ConfirmOverwriteDelegate confirmDelegate, string fileFilter, string directoryFilter, bool restoreDateTime) { if ((overwrite == Overwrite.Prompt) && (confirmDelegate == null)) { throw new ArgumentNullException("confirmDelegate"); } continueRunning_ = true; overwrite_ = overwrite; confirmDelegate_ = confirmDelegate; extractNameTransform_ = new WindowsNameTransform(targetDirectory); fileFilter_ = new NameFilter(fileFilter); directoryFilter_ = new NameFilter(directoryFilter); restoreDateTimeOnExtract_ = restoreDateTime; using (blubbFile_ = new BlubbZipFile(blubbFileName)) { #if !NETCF_1_0 if (password_ != null) { blubbFile_.Password = password_; } #endif System.Collections.IEnumerator enumerator = blubbFile_.GetEnumerator(); while (continueRunning_ && enumerator.MoveNext()) { BlubbZipEntry entry = (BlubbZipEntry)enumerator.Current; if (entry.IsFile) { // TODO Path.GetDirectory can fail here on invalid characters. if (directoryFilter_.IsMatch(Path.GetDirectoryName(entry.Name)) && fileFilter_.IsMatch(entry.Name)) { ExtractEntry(entry); } } else if (entry.IsDirectory) { if (directoryFilter_.IsMatch(entry.Name) && CreateEmptyDirectories) { ExtractEntry(entry); } } else { // Do nothing for volume labels etc... } } } }
/// <summary> /// Creates a copy of this blubb entry. /// </summary> /// <returns>An <see cref="Object"/> that is a copy of the current instance.</returns> public object Clone() { BlubbZipEntry result = (BlubbZipEntry)this.MemberwiseClone(); // Ensure extra data is unique if it exists. if (extra != null) { result.extra = new byte[extra.Length]; Array.Copy(extra, 0, result.extra, 0, extra.Length); } return(result); }
void ProcessFile(object sender, ScanEventArgs e) { if ((events_ != null) && (events_.ProcessFile != null)) { events_.ProcessFile(sender, e); } if (e.ContinueRunning) { using (FileStream stream = File.OpenRead(e.Name)) { BlubbZipEntry entry = entryFactory_.MakeFileEntry(e.Name); outputStream_.PutNextEntry(entry); AddFileContents(e.Name, stream); } } }
void ProcessDirectory(object sender, DirectoryEventArgs e) { if (!e.HasMatchingFiles && CreateEmptyDirectories) { if (events_ != null) { events_.OnProcessDirectory(e.Name, e.HasMatchingFiles); } if (e.ContinueRunning) { if (e.Name != sourceDirectory_) { BlubbZipEntry entry = entryFactory_.MakeDirectoryEntry(e.Name); outputStream_.PutNextEntry(entry); } } } }
/// <summary> /// Make a new <see cref="BlubbEntry"/> from a name. /// </summary> /// <param name="fileName">The name of the file to create a new entry for.</param> /// <param name="useFileSystem">If true entry detail is retrieved from the file system if the file exists.</param> /// <returns>Returns a new <see cref="BlubbEntry"/> based on the <paramref name="fileName"/>.</returns> public BlubbZipEntry MakeFileEntry(string fileName, bool useFileSystem) { BlubbZipEntry result = new BlubbZipEntry(nameTransform_.TransformFile(fileName)); result.IsUnicodeText = isUnicodeText_; int externalAttributes = 0; bool useAttributes = (setAttributes_ != 0); FileInfo fi = null; if (useFileSystem) { fi = new FileInfo(fileName); } if (fi != null && fi.Exists) { switch (timeSetting_) { case TimeSetting.CreateTime: result.DateTime = fi.CreationTime; break; case TimeSetting.CreateTimeUtc: result.DateTime = fi.CreationTimeUtc; break; case TimeSetting.LastAccessTime: result.DateTime = fi.LastAccessTime; break; case TimeSetting.LastAccessTimeUtc: result.DateTime = fi.LastAccessTimeUtc; break; case TimeSetting.LastWriteTime: result.DateTime = fi.LastWriteTime; break; case TimeSetting.LastWriteTimeUtc: result.DateTime = fi.LastWriteTimeUtc; break; case TimeSetting.Fixed: result.DateTime = fixedDateTime_; break; default: throw new BlubbZipException("Unhandled time setting in MakeFileEntry"); } result.Size = fi.Length; useAttributes = true; externalAttributes = ((int)fi.Attributes & getAttributes_); } else { if (timeSetting_ == TimeSetting.Fixed) { result.DateTime = fixedDateTime_; } } if (useAttributes) { externalAttributes |= setAttributes_; result.ExternalFileAttributes = externalAttributes; } return(result); }
/// <summary> /// Initialises a default <see cref="BlubbFile"/> instance with no entries and no file storage. /// </summary> internal BlubbZipFile() { entries_ = new BlubbZipEntry[ 0 ]; isNewArchive_ = true; }
/// <summary> /// Test a local header against that provided from the central directory /// </summary> /// <param name="entry"> /// The entry to test against /// </param> /// <param name="tests">The type of <see cref="HeaderTest">tests</see> to carry out.</param> /// <returns>The offset of the entries data in the file</returns> long TestLocalHeader( BlubbZipEntry entry, HeaderTest tests ) { lock( baseStream_ ) { bool testHeader = ( tests & HeaderTest.Header ) != 0; bool testData = ( tests & HeaderTest.Extract ) != 0; baseStream_.Seek( offsetOfFirstEntry + entry.Offset, SeekOrigin.Begin ); if( (int)ReadLEUint() != BlubbZipConstants.LocalHeaderSignature ) { throw new BlubbZipException( string.Format( "Wrong local header signature @{0:X}", offsetOfFirstEntry + entry.Offset ) ); } short extractVersion = (short)ReadLEUshort(); short localFlags = (short)ReadLEUshort(); short compressionMethod = (short)ReadLEUshort(); short fileTime = (short)ReadLEUshort(); short fileDate = (short)ReadLEUshort(); uint crcValue = ReadLEUint(); long compressedSize = ReadLEUint(); long size = ReadLEUint(); int storedNameLength = ReadLEUshort(); int extraDataLength = ReadLEUshort(); byte[] nameData = new byte[ storedNameLength ]; StreamUtils.ReadFully( baseStream_, nameData ); byte[] extraData = new byte[ extraDataLength ]; StreamUtils.ReadFully( baseStream_, extraData ); BlubbZipExtraData localExtraData = new BlubbZipExtraData( extraData ); // Extra data / blubb64 checks if( localExtraData.Find( 1 ) ) { // TODO Check for tag values being distinct.. Multiple blubb64 tags means what? // Blubb64 extra data but 'extract version' is too low if( extractVersion < BlubbZipConstants.VersionBlubb64 ) { throw new BlubbZipException( string.Format( "Extra data contains Blubb64 information but version {0}.{1} is not high enough", extractVersion / 10, extractVersion % 10 ) ); } // Blubb64 extra data but size fields dont indicate its required. if( ( (uint)size != uint.MaxValue ) && ( (uint)compressedSize != uint.MaxValue ) ) { throw new BlubbZipException( "Entry sizes not correct for Blubb64" ); } size = localExtraData.ReadLong(); compressedSize = localExtraData.ReadLong(); if( ( localFlags & (int)GeneralBitFlags.Descriptor ) != 0 ) { // These may be valid if patched later if( ( size != -1 ) && ( size != entry.Size ) ) { throw new BlubbZipException( "Size invalid for descriptor" ); } if( ( compressedSize != -1 ) && ( compressedSize != entry.CompressedSize ) ) { throw new BlubbZipException( "Compressed size invalid for descriptor" ); } } } else { // No blubb64 extra data but entry requires it. if( ( extractVersion >= BlubbZipConstants.VersionBlubb64 ) && ( ( (uint)size == uint.MaxValue ) || ( (uint)compressedSize == uint.MaxValue ) ) ) { throw new BlubbZipException( "Required Blubb64 extended information missing" ); } } if( testData ) { if( entry.IsFile ) { if( !entry.IsCompressionMethodSupported() ) { throw new BlubbZipException( "Compression method not supported" ); } if( ( extractVersion > BlubbZipConstants.VersionMadeBy ) || ( ( extractVersion > 20 ) && ( extractVersion < BlubbZipConstants.VersionBlubb64 ) ) ) { throw new BlubbZipException( string.Format( "Version required to extract this entry not supported ({0})", extractVersion ) ); } if( ( localFlags & (int)( GeneralBitFlags.Patched | GeneralBitFlags.StrongEncryption | GeneralBitFlags.EnhancedCompress | GeneralBitFlags.HeaderMasked ) ) != 0 ) { throw new BlubbZipException( "The library does not support the blubb version required to extract this entry" ); } } } if( testHeader ) { if( ( extractVersion <= 63 ) && ( extractVersion != 10 ) && ( extractVersion != 11 ) && ( extractVersion != 20 ) && ( extractVersion != 21 ) && ( extractVersion != 25 ) && ( extractVersion != 27 ) && ( extractVersion != 45 ) && ( extractVersion != 46 ) && ( extractVersion != 50 ) && ( extractVersion != 51 ) && ( extractVersion != 52 ) && ( extractVersion != 61 ) && ( extractVersion != 62 ) && ( extractVersion != 63 ) ) { throw new BlubbZipException( string.Format( "Version required to extract this entry is invalid ({0})", extractVersion ) ); } // Local entry flags dont have reserved bit set on. if( ( localFlags & (int)( GeneralBitFlags.ReservedPKware4 | GeneralBitFlags.ReservedPkware14 | GeneralBitFlags.ReservedPkware15 ) ) != 0 ) { throw new BlubbZipException( "Reserved bit flags cannot be set." ); } // Encryption requires extract version >= 20 if( ( ( localFlags & (int)GeneralBitFlags.Encrypted ) != 0 ) && ( extractVersion < 20 ) ) { throw new BlubbZipException( string.Format( "Version required to extract this entry is too low for encryption ({0})", extractVersion ) ); } // Strong encryption requires encryption flag to be set and extract version >= 50. if( ( localFlags & (int)GeneralBitFlags.StrongEncryption ) != 0 ) { if( ( localFlags & (int)GeneralBitFlags.Encrypted ) == 0 ) { throw new BlubbZipException( "Strong encryption flag set but encryption flag is not set" ); } if( extractVersion < 50 ) { throw new BlubbZipException( string.Format( "Version required to extract this entry is too low for encryption ({0})", extractVersion ) ); } } // Patched entries require extract version >= 27 if( ( ( localFlags & (int)GeneralBitFlags.Patched ) != 0 ) && ( extractVersion < 27 ) ) { throw new BlubbZipException( string.Format( "Patched data requires higher version than ({0})", extractVersion ) ); } // Central header flags match local entry flags. if( localFlags != entry.Flags ) { throw new BlubbZipException( "Central header/local header flags mismatch" ); } // Central header compression method matches local entry if( entry.CompressionMethod != (CompressionMethod)compressionMethod ) { throw new BlubbZipException( "Central header/local header compression method mismatch" ); } if( entry.Version != extractVersion ) { throw new BlubbZipException( "Extract version mismatch" ); } // Strong encryption and extract version match if( ( localFlags & (int)GeneralBitFlags.StrongEncryption ) != 0 ) { if( extractVersion < 62 ) { throw new BlubbZipException( "Strong encryption flag set but version not high enough" ); } } if( ( localFlags & (int)GeneralBitFlags.HeaderMasked ) != 0 ) { if( ( fileTime != 0 ) || ( fileDate != 0 ) ) { throw new BlubbZipException( "Header masked set but date/time values non-zero" ); } } if( ( localFlags & (int)GeneralBitFlags.Descriptor ) == 0 ) { if( crcValue != (uint)entry.Crc ) { throw new BlubbZipException( "Central header/local header crc mismatch" ); } } // Crc valid for empty entry. // This will also apply to streamed entries where size isnt known and the header cant be patched if( ( size == 0 ) && ( compressedSize == 0 ) ) { if( crcValue != 0 ) { throw new BlubbZipException( "Invalid CRC for empty entry" ); } } // TODO: make test more correct... can't compare lengths as was done originally as this can fail for MBCS strings // Assuming a code page at this point is not valid? Best is to store the name length in the BlubbEntry probably if( entry.Name.Length > storedNameLength ) { throw new BlubbZipException( "File name length mismatch" ); } // Name data has already been read convert it and compare. string localName = BlubbZipConstants.ConvertToStringExt( localFlags, nameData ); // Central directory and local entry name match if( localName != entry.Name ) { throw new BlubbZipException( "Central header and local header file name mismatch" ); } // Directories have zero actual size but can have compressed size if( entry.IsDirectory ) { if( size > 0 ) { throw new BlubbZipException( "Directory cannot have size" ); } // There may be other cases where the compressed size can be greater than this? // If so until details are known we will be strict. if( entry.IsCrypted ) { if( compressedSize > BlubbZipConstants.CryptoHeaderSize + 2 ) { throw new BlubbZipException( "Directory compressed size invalid" ); } } else if( compressedSize > 2 ) { // When not compressed the directory size can validly be 2 bytes // if the true size wasnt known when data was originally being written. // NOTE: Versions of the library 0.85.4 and earlier always added 2 bytes throw new BlubbZipException( "Directory compressed size invalid" ); } } if( !BlubbZipNameTransform.IsValidName( localName, true ) ) { throw new BlubbZipException( "Name is invalid" ); } } // Tests that apply to both data and header. // Size can be verified only if it is known in the local header. // it will always be known in the central header. if( ( ( localFlags & (int)GeneralBitFlags.Descriptor ) == 0 ) || ( ( size > 0 ) || ( compressedSize > 0 ) ) ) { if( size != entry.Size ) { throw new BlubbZipException( string.Format( "Size mismatch between central header({0}) and local header({1})", entry.Size, size ) ); } if( compressedSize != entry.CompressedSize ) { throw new BlubbZipException( string.Format( "Compressed size mismatch between central header({0}) and local header({1})", entry.CompressedSize, compressedSize ) ); } } int extraLength = storedNameLength + extraDataLength; return offsetOfFirstEntry + entry.Offset + BlubbZipConstants.LocalHeaderBaseSize + extraLength; } }
public BlubbUpdate( BlubbZipEntry original, BlubbZipEntry updated ) { throw new BlubbZipException( "Modify not currently supported" ); /* command_ = UpdateCommand.Modify; entry_ = ( BlubbEntry )original.Clone(); outEntry_ = ( BlubbEntry )updated.Clone(); */ }
// Write the local file header // TODO: BlubbHelperStream.WriteLocalHeader is not yet used and needs checking for BlubbFile and BlubbOuptutStream usage void WriteLocalHeader( BlubbZipEntry entry, EntryPatchData patchData ) { CompressionMethod method = entry.CompressionMethod; bool headerInfoAvailable = true; // How to get this? bool patchEntryHeader = false; WriteLEInt( BlubbZipConstants.LocalHeaderSignature ); WriteLEShort( entry.Version ); WriteLEShort( entry.Flags ); WriteLEShort( (byte)method ); WriteLEInt( (int)entry.DosTime ); if( headerInfoAvailable == true ) { WriteLEInt( (int)entry.Crc ); if( entry.LocalHeaderRequiresBlubb64 ) { WriteLEInt( -1 ); WriteLEInt( -1 ); } else { WriteLEInt( entry.IsCrypted ? (int)entry.CompressedSize + BlubbZipConstants.CryptoHeaderSize : (int)entry.CompressedSize ); WriteLEInt( (int)entry.Size ); } } else { if( patchData != null ) { patchData.CrcPatchOffset = stream_.Position; } WriteLEInt( 0 ); // Crc if( patchData != null ) { patchData.SizePatchOffset = stream_.Position; } // For local header both sizes appear in Blubb64 Extended Information if( entry.LocalHeaderRequiresBlubb64 && patchEntryHeader ) { WriteLEInt( -1 ); WriteLEInt( -1 ); } else { WriteLEInt( 0 ); // Compressed size WriteLEInt( 0 ); // Uncompressed size } } byte[] name = BlubbZipConstants.ConvertToArray( entry.Flags, entry.Name ); if( name.Length > 0xFFFF ) { throw new BlubbZipException( "Entry name too long." ); } BlubbZipExtraData ed = new BlubbZipExtraData( entry.ExtraData ); if( entry.LocalHeaderRequiresBlubb64 && ( headerInfoAvailable || patchEntryHeader ) ) { ed.StartNewEntry(); if( headerInfoAvailable ) { ed.AddLeLong( entry.Size ); ed.AddLeLong( entry.CompressedSize ); } else { ed.AddLeLong( -1 ); ed.AddLeLong( -1 ); } ed.AddNewEntry( 1 ); if( !ed.Find( 1 ) ) { throw new BlubbZipException( "Internal error cant find extra data" ); } if( patchData != null ) { patchData.SizePatchOffset = ed.CurrentReadIndex; } } else { ed.Delete( 1 ); } byte[] extra = ed.GetEntryData(); WriteLEShort( name.Length ); WriteLEShort( extra.Length ); if( name.Length > 0 ) { stream_.Write( name, 0, name.Length ); } if( entry.LocalHeaderRequiresBlubb64 && patchEntryHeader ) { patchData.SizePatchOffset += stream_.Position; } if( extra.Length > 0 ) { stream_.Write( extra, 0, extra.Length ); } }
// Constructors public BlubbUpdate( string fileName, BlubbZipEntry entry ) { command_ = UpdateCommand.Add; entry_ = entry; filename_ = fileName; }
public BlubbUpdate( IStaticDataSource dataSource, string entryName, CompressionMethod compressionMethod ) { command_ = UpdateCommand.Add; entry_ = new BlubbZipEntry( entryName ); entry_.CompressionMethod = compressionMethod; dataSource_ = dataSource; }
/// <summary> /// Make a new <see cref="BlubbEntry"></see> for a directory. /// </summary> /// <param name="directoryName">The raw untransformed name for the new directory</param> /// <param name="useFileSystem">If true entry detail is retrieved from the file system if the file exists.</param> /// <returns>Returns a new <see cref="BlubbEntry"></see> representing a directory.</returns> public BlubbZipEntry MakeDirectoryEntry(string directoryName, bool useFileSystem) { BlubbZipEntry result = new BlubbZipEntry(nameTransform_.TransformDirectory(directoryName)); result.Size = 0; int externalAttributes = 0; DirectoryInfo di = null; if (useFileSystem) { di = new DirectoryInfo(directoryName); } if (di != null && di.Exists) { switch (timeSetting_) { case TimeSetting.CreateTime: result.DateTime = di.CreationTime; break; case TimeSetting.CreateTimeUtc: result.DateTime = di.CreationTimeUtc; break; case TimeSetting.LastAccessTime: result.DateTime = di.LastAccessTime; break; case TimeSetting.LastAccessTimeUtc: result.DateTime = di.LastAccessTimeUtc; break; case TimeSetting.LastWriteTime: result.DateTime = di.LastWriteTime; break; case TimeSetting.LastWriteTimeUtc: result.DateTime = di.LastWriteTimeUtc; break; case TimeSetting.Fixed: result.DateTime = fixedDateTime_; break; default: throw new BlubbZipException("Unhandled time setting in MakeDirectoryEntry"); } externalAttributes = ((int)di.Attributes & getAttributes_); } else { if (timeSetting_ == TimeSetting.Fixed) { result.DateTime = fixedDateTime_; } } // Always set directory attribute on. externalAttributes |= (setAttributes_ | 16); result.ExternalFileAttributes = externalAttributes; return(result); }
/// <summary> /// Copy an existing entry. /// </summary> /// <param name="entry">The existing entry to copy.</param> public BlubbUpdate( BlubbZipEntry entry ) : this( UpdateCommand.Copy, entry ) { // Do nothing. }
/// <summary> /// Closes the blubb input stream /// </summary> public override void Close() { internalReader = new ReadDataHandler( ReadingNotAvailable ); crc = null; entry = null; base.Close(); }
/// <summary> /// Advances to the next entry in the archive /// </summary> /// <returns> /// The next <see cref="BlubbEntry">entry</see> in the archive or null if there are no more entries. /// </returns> /// <remarks> /// If the previous entry is still open <see cref="CloseEntry">CloseEntry</see> is called. /// </remarks> /// <exception cref="InvalidOperationException"> /// Input stream is closed /// </exception> /// <exception cref="BlubbException"> /// Password is not set, password is invalid, compression method is invalid, /// version required to extract is not supported /// </exception> public BlubbZipEntry GetNextEntry() { if (crc == null) { throw new InvalidOperationException("Closed."); } if (entry != null) { CloseEntry(); } int header = inputBuffer.ReadLeInt(); if (header == BlubbZipConstants.CentralHeaderSignature || header == BlubbZipConstants.EndOfCentralDirectorySignature || header == BlubbZipConstants.CentralHeaderDigitalSignature || header == BlubbZipConstants.ArchiveExtraDataSignature || header == BlubbZipConstants.Blubb64CentralFileHeaderSignature) { // No more individual entries exist Close(); return(null); } // -jr- 07-Dec-2003 Ignore spanning temporary signatures if found // Spanning signature is same as descriptor signature and is untested as yet. if ((header == BlubbZipConstants.SpanningTempSignature) || (header == BlubbZipConstants.SpanningSignature)) { header = inputBuffer.ReadLeInt(); } if (header != BlubbZipConstants.LocalHeaderSignature) { throw new BlubbZipException("Wrong Local header signature: 0x" + String.Format("{0:X}", header)); } short versionRequiredToExtract = (short)inputBuffer.ReadLeShort(); flags = inputBuffer.ReadLeShort(); method = inputBuffer.ReadLeShort(); uint dostime = (uint)inputBuffer.ReadLeInt(); int crc2 = inputBuffer.ReadLeInt(); csize = inputBuffer.ReadLeInt(); size = inputBuffer.ReadLeInt(); int nameLen = inputBuffer.ReadLeShort(); int extraLen = inputBuffer.ReadLeShort(); bool isCrypted = (flags & 1) == 1; byte[] buffer = new byte[nameLen]; inputBuffer.ReadRawBuffer(buffer); string name = BlubbZipConstants.ConvertToStringExt(flags, buffer); entry = new BlubbZipEntry(name, versionRequiredToExtract); entry.Flags = flags; entry.CompressionMethod = (CompressionMethod)method; if ((flags & 8) == 0) { entry.Crc = crc2 & 0xFFFFFFFFL; entry.Size = size & 0xFFFFFFFFL; entry.CompressedSize = csize & 0xFFFFFFFFL; entry.CryptoCheckValue = (byte)((crc2 >> 24) & 0xff); } else { // This allows for GNU, WinBlubb and possibly other archives, the PKZIP spec // says these values are zero under these circumstances. if (crc2 != 0) { entry.Crc = crc2 & 0xFFFFFFFFL; } if (size != 0) { entry.Size = size & 0xFFFFFFFFL; } if (csize != 0) { entry.CompressedSize = csize & 0xFFFFFFFFL; } entry.CryptoCheckValue = (byte)((dostime >> 8) & 0xff); } entry.DosTime = dostime; // If local header requires Blubb64 is true then the extended header should contain // both values. // Handle extra data if present. This can set/alter some fields of the entry. if (extraLen > 0) { byte[] extra = new byte[extraLen]; inputBuffer.ReadRawBuffer(extra); entry.ExtraData = extra; } entry.ProcessExtraData(true); if (entry.CompressedSize >= 0) { csize = entry.CompressedSize; } if (entry.Size >= 0) { size = entry.Size; } if (method == (int)CompressionMethod.Stored && (!isCrypted && csize != size || (isCrypted && csize - BlubbZipConstants.CryptoHeaderSize != size))) { throw new BlubbZipException("Stored, but compressed != uncompressed"); } // Determine how to handle reading of data if this is attempted. if (entry.IsCompressionMethodSupported()) { internalReader = new ReadDataHandler(InitialRead); } else { internalReader = new ReadDataHandler(ReadingNotSupported); } return(entry); }
/// <summary> /// Advances to the next entry in the archive /// </summary> /// <returns> /// The next <see cref="BlubbEntry">entry</see> in the archive or null if there are no more entries. /// </returns> /// <remarks> /// If the previous entry is still open <see cref="CloseEntry">CloseEntry</see> is called. /// </remarks> /// <exception cref="InvalidOperationException"> /// Input stream is closed /// </exception> /// <exception cref="BlubbException"> /// Password is not set, password is invalid, compression method is invalid, /// version required to extract is not supported /// </exception> public BlubbZipEntry GetNextEntry() { if( crc == null ) { throw new InvalidOperationException( "Closed." ); } if( entry != null ) { CloseEntry(); } int header = inputBuffer.ReadLeInt(); if( header == BlubbZipConstants.CentralHeaderSignature || header == BlubbZipConstants.EndOfCentralDirectorySignature || header == BlubbZipConstants.CentralHeaderDigitalSignature || header == BlubbZipConstants.ArchiveExtraDataSignature || header == BlubbZipConstants.Blubb64CentralFileHeaderSignature ) { // No more individual entries exist Close(); return null; } // -jr- 07-Dec-2003 Ignore spanning temporary signatures if found // Spanning signature is same as descriptor signature and is untested as yet. if( ( header == BlubbZipConstants.SpanningTempSignature ) || ( header == BlubbZipConstants.SpanningSignature ) ) { header = inputBuffer.ReadLeInt(); } if( header != BlubbZipConstants.LocalHeaderSignature ) { throw new BlubbZipException( "Wrong Local header signature: 0x" + String.Format( "{0:X}", header ) ); } short versionRequiredToExtract = (short)inputBuffer.ReadLeShort(); flags = inputBuffer.ReadLeShort(); method = inputBuffer.ReadLeShort(); uint dostime = (uint)inputBuffer.ReadLeInt(); int crc2 = inputBuffer.ReadLeInt(); csize = inputBuffer.ReadLeInt(); size = inputBuffer.ReadLeInt(); int nameLen = inputBuffer.ReadLeShort(); int extraLen = inputBuffer.ReadLeShort(); bool isCrypted = ( flags & 1 ) == 1; byte[] buffer = new byte[ nameLen ]; inputBuffer.ReadRawBuffer( buffer ); string name = BlubbZipConstants.ConvertToStringExt( flags, buffer ); entry = new BlubbZipEntry( name, versionRequiredToExtract ); entry.Flags = flags; entry.CompressionMethod = (CompressionMethod)method; if( ( flags & 8 ) == 0 ) { entry.Crc = crc2 & 0xFFFFFFFFL; entry.Size = size & 0xFFFFFFFFL; entry.CompressedSize = csize & 0xFFFFFFFFL; entry.CryptoCheckValue = (byte)( ( crc2 >> 24 ) & 0xff ); } else { // This allows for GNU, WinBlubb and possibly other archives, the PKZIP spec // says these values are zero under these circumstances. if( crc2 != 0 ) { entry.Crc = crc2 & 0xFFFFFFFFL; } if( size != 0 ) { entry.Size = size & 0xFFFFFFFFL; } if( csize != 0 ) { entry.CompressedSize = csize & 0xFFFFFFFFL; } entry.CryptoCheckValue = (byte)( ( dostime >> 8 ) & 0xff ); } entry.DosTime = dostime; // If local header requires Blubb64 is true then the extended header should contain // both values. // Handle extra data if present. This can set/alter some fields of the entry. if( extraLen > 0 ) { byte[] extra = new byte[ extraLen ]; inputBuffer.ReadRawBuffer( extra ); entry.ExtraData = extra; } entry.ProcessExtraData( true ); if( entry.CompressedSize >= 0 ) { csize = entry.CompressedSize; } if( entry.Size >= 0 ) { size = entry.Size; } if( method == (int)CompressionMethod.Stored && ( !isCrypted && csize != size || ( isCrypted && csize - BlubbZipConstants.CryptoHeaderSize != size ) ) ) { throw new BlubbZipException( "Stored, but compressed != uncompressed" ); } // Determine how to handle reading of data if this is attempted. if( entry.IsCompressionMethodSupported() ) { internalReader = new ReadDataHandler( InitialRead ); } else { internalReader = new ReadDataHandler( ReadingNotSupported ); } return entry; }
/// <summary> /// Closes the current entry, updating header and footer information as required /// </summary> /// <exception cref="System.IO.IOException"> /// An I/O error occurs. /// </exception> /// <exception cref="System.InvalidOperationException"> /// No entry is active. /// </exception> public void CloseEntry() { if (curEntry == null) { throw new InvalidOperationException("No open entry"); } long csize = size; // First finish the deflater, if appropriate if (curMethod == CompressionMethod.Deflated) { if (size > 0) { base.Finish(); csize = deflater_.TotalOut; } else { deflater_.Reset(); } } if (curEntry.Size < 0) { curEntry.Size = size; } else if (curEntry.Size != size) { throw new BlubbZipException("size was " + size + ", but I expected " + curEntry.Size); } if (curEntry.CompressedSize < 0) { curEntry.CompressedSize = csize; } else if (curEntry.CompressedSize != csize) { throw new BlubbZipException("compressed size was " + csize + ", but I expected " + curEntry.CompressedSize); } if (curEntry.Crc < 0) { curEntry.Crc = crc.Value; } else if (curEntry.Crc != crc.Value) { throw new BlubbZipException("crc was " + crc.Value + ", but I expected " + curEntry.Crc); } offset += csize; if (curEntry.IsCrypted == true) { curEntry.CompressedSize += BlubbZipConstants.CryptoHeaderSize; } // Patch the header if possible if (patchEntryHeader == true) { patchEntryHeader = false; long curPos = baseOutputStream_.Position; baseOutputStream_.Seek(crcPatchPos, SeekOrigin.Begin); WriteLeInt((int)curEntry.Crc); if (curEntry.LocalHeaderRequiresBlubb64) { if (sizePatchPos == -1) { throw new BlubbZipException("Entry requires blubb64 but this has been turned off"); } baseOutputStream_.Seek(sizePatchPos, SeekOrigin.Begin); WriteLeLong(curEntry.Size); WriteLeLong(curEntry.CompressedSize); } else { WriteLeInt((int)curEntry.CompressedSize); WriteLeInt((int)curEntry.Size); } baseOutputStream_.Seek(curPos, SeekOrigin.Begin); } // Add data descriptor if flagged as required if ((curEntry.Flags & 8) != 0) { WriteLeInt(BlubbZipConstants.DataDescriptorSignature); WriteLeInt(unchecked ((int)curEntry.Crc)); if (curEntry.LocalHeaderRequiresBlubb64) { WriteLeLong(curEntry.CompressedSize); WriteLeLong(curEntry.Size); offset += BlubbZipConstants.Blubb64DataDescriptorSize; } else { WriteLeInt((int)curEntry.CompressedSize); WriteLeInt((int)curEntry.Size); offset += BlubbZipConstants.DataDescriptorSize; } } entries.Add(curEntry); curEntry = null; }
/// <summary> /// Starts a new Blubb entry. It automatically closes the previous /// entry if present. /// All entry elements bar name are optional, but must be correct if present. /// If the compression method is stored and the output is not patchable /// the compression for that entry is automatically changed to deflate level 0 /// </summary> /// <param name="entry"> /// the entry. /// </param> /// <exception cref="System.ArgumentNullException"> /// if entry passed is null. /// </exception> /// <exception cref="System.IO.IOException"> /// if an I/O error occured. /// </exception> /// <exception cref="System.InvalidOperationException"> /// if stream was finished /// </exception> /// <exception cref="BlubbException"> /// Too many entries in the Blubb file<br/> /// Entry name is too long<br/> /// Finish has already been called<br/> /// </exception> public void PutNextEntry(BlubbZipEntry entry) { if (entry == null) { throw new ArgumentNullException("entry"); } if (entries == null) { throw new InvalidOperationException("BlubbOutputStream was finished"); } if (curEntry != null) { CloseEntry(); } if (entries.Count == int.MaxValue) { throw new BlubbZipException("Too many entries for Blubb file"); } CompressionMethod method = entry.CompressionMethod; int compressionLevel = defaultCompressionLevel; // Clear flags that the library manages internally entry.Flags &= (int)GeneralBitFlags.UnicodeText; patchEntryHeader = false; bool headerInfoAvailable; // No need to compress - definitely no data. if (entry.Size == 0) { entry.CompressedSize = entry.Size; entry.Crc = 0; method = CompressionMethod.Stored; headerInfoAvailable = true; } else { headerInfoAvailable = (entry.Size >= 0) && entry.HasCrc; // Switch to deflation if storing isnt possible. if (method == CompressionMethod.Stored) { if (!headerInfoAvailable) { if (!CanPatchEntries) { // Can't patch entries so storing is not possible. method = CompressionMethod.Deflated; compressionLevel = 0; } } else { // entry.size must be > 0 entry.CompressedSize = entry.Size; headerInfoAvailable = entry.HasCrc; } } } if (headerInfoAvailable == false) { if (CanPatchEntries == false) { // Only way to record size and compressed size is to append a data descriptor // after compressed data. // Stored entries of this form have already been converted to deflating. entry.Flags |= 8; } else { patchEntryHeader = true; } } if (Password != null) { entry.IsCrypted = true; if (entry.Crc < 0) { // Need to append a data descriptor as the crc isnt available for use // with encryption, the date is used instead. Setting the flag // indicates this to the decompressor. entry.Flags |= 8; } } entry.Offset = offset; entry.CompressionMethod = (CompressionMethod)method; curMethod = method; sizePatchPos = -1; if (useBlubb64_ == UseBlubb64.On || (entry.Size < 0 && useBlubb64_ == UseBlubb64.Dynamic)) { entry.ForceBlubb64(); } // Write the local file header WriteLeInt(BlubbZipConstants.LocalHeaderSignature); WriteLeShort(entry.Version); WriteLeShort(entry.Flags); WriteLeShort((byte)method); WriteLeInt((int)entry.DosTime); // TODO: Refactor header writing. Its done in several places. if (headerInfoAvailable == true) { WriteLeInt((int)entry.Crc); if (entry.LocalHeaderRequiresBlubb64) { WriteLeInt(-1); WriteLeInt(-1); } else { WriteLeInt(entry.IsCrypted ? (int)entry.CompressedSize + BlubbZipConstants.CryptoHeaderSize : (int)entry.CompressedSize); WriteLeInt((int)entry.Size); } } else { if (patchEntryHeader == true) { crcPatchPos = baseOutputStream_.Position; } WriteLeInt(0); // Crc if (patchEntryHeader) { sizePatchPos = baseOutputStream_.Position; } // For local header both sizes appear in Blubb64 Extended Information if (entry.LocalHeaderRequiresBlubb64 || patchEntryHeader) { WriteLeInt(-1); WriteLeInt(-1); } else { WriteLeInt(0); // Compressed size WriteLeInt(0); // Uncompressed size } } byte[] name = BlubbZipConstants.ConvertToArray(entry.Flags, entry.Name); if (name.Length > 0xFFFF) { throw new BlubbZipException("Entry name too long."); } BlubbZipExtraData ed = new BlubbZipExtraData(entry.ExtraData); if (entry.LocalHeaderRequiresBlubb64) { ed.StartNewEntry(); if (headerInfoAvailable) { ed.AddLeLong(entry.Size); ed.AddLeLong(entry.CompressedSize); } else { ed.AddLeLong(-1); ed.AddLeLong(-1); } ed.AddNewEntry(1); if (!ed.Find(1)) { throw new BlubbZipException("Internal error cant find extra data"); } if (patchEntryHeader) { sizePatchPos = ed.CurrentReadIndex; } } else { ed.Delete(1); } byte[] extra = ed.GetEntryData(); WriteLeShort(name.Length); WriteLeShort(extra.Length); if (name.Length > 0) { baseOutputStream_.Write(name, 0, name.Length); } if (entry.LocalHeaderRequiresBlubb64 && patchEntryHeader) { sizePatchPos += baseOutputStream_.Position; } if (extra.Length > 0) { baseOutputStream_.Write(extra, 0, extra.Length); } offset += BlubbZipConstants.LocalHeaderBaseSize + name.Length + extra.Length; // Activate the entry. curEntry = entry; crc.Reset(); if (method == CompressionMethod.Deflated) { deflater_.Reset(); deflater_.SetLevel(compressionLevel); } size = 0; if (entry.IsCrypted == true) { if (entry.Crc < 0) // so testing Blubb will says its ok { WriteEncryptionHeader(entry.DosTime << 16); } else { WriteEncryptionHeader(entry.Crc); } } }
int FindExistingUpdate( BlubbZipEntry entry ) { int result = -1; string convertedName = GetTransformedFileName( entry.Name ); if( updateIndex_.ContainsKey( convertedName ) ) { result = (int)updateIndex_[ convertedName ]; } /* // This is slow like the coming of the next ice age but takes less storage and may be useful // for CF? for (int index = 0; index < updates_.Count; ++index) { BlubbUpdate zu = ( BlubbUpdate )updates_[index]; if ( (zu.Entry.BlubbFileIndex == entry.BlubbFileIndex) && (string.Compare(convertedName, zu.Entry.Name, true, CultureInfo.InvariantCulture) == 0) ) { result = index; break; } } */ return result; }
/// <summary> /// Search for and read the central directory of a blubb file filling the entries array. /// </summary> /// <exception cref="System.IO.IOException"> /// An i/o error occurs. /// </exception> /// <exception cref="GodLesZ.Library.BlubbZip.Blubb.BlubbException"> /// The central directory is malformed or cannot be found /// </exception> void ReadEntries() { // Search for the End Of Central Directory. When a blubb comment is // present the directory will start earlier // // The search is limited to 64K which is the maximum size of a trailing comment field to aid speed. // This should be compatible with both SFX and ZIP files but has only been tested for Blubb files // If a SFX file has the Blubb data attached as a resource and there are other resources occuring later then // this could be invalid. // Could also speed this up by reading memory in larger blocks. if( baseStream_.CanSeek == false ) { throw new BlubbZipException( "BlubbFile stream must be seekable" ); } long locatedEndOfCentralDir = LocateBlockWithSignature( BlubbZipConstants.EndOfCentralDirectorySignature, baseStream_.Length, BlubbZipConstants.EndOfCentralRecordBaseSize, 0xffff ); if( locatedEndOfCentralDir < 0 ) throw new BlubbZipException( "Cannot find central directory" ); // Read end of central directory record ushort thisDiskNumber = ReadLEUshort(); ushort startCentralDirDisk = ReadLEUshort(); ulong entriesForThisDisk = ReadLEUshort(); ulong entriesForWholeCentralDir = ReadLEUshort(); ulong centralDirSize = ReadLEUint(); long offsetOfCentralDir = ReadLEUint(); uint commentSize = ReadLEUshort(); if( commentSize > 0 ) { byte[] comment = new byte[ commentSize ]; StreamUtils.ReadFully( baseStream_, comment ); comment_ = BlubbZipConstants.ConvertToString( comment ); } else { comment_ = string.Empty; } bool isBlubb64 = false; // Check if blubb64 header information is required. if( ( thisDiskNumber == 0xffff ) || ( startCentralDirDisk == 0xffff ) || ( entriesForThisDisk == 0xffff ) || ( entriesForWholeCentralDir == 0xffff ) || ( centralDirSize == 0xffffffff ) || ( offsetOfCentralDir == 0xffffffff ) ) { isBlubb64 = true; long offset = LocateBlockWithSignature( BlubbZipConstants.Blubb64CentralDirLocatorSignature, locatedEndOfCentralDir, 0, 0x1000 ); if( offset < 0 ) { throw new BlubbZipException( "Cannot find Blubb64 locator" ); } // number of the disk with the start of the blubb64 end of central directory 4 bytes // relative offset of the blubb64 end of central directory record 8 bytes // total number of disks 4 bytes ReadLEUint(); // startDisk64 is not currently used ulong offset64 = ReadLEUlong(); uint totalDisks = ReadLEUint(); baseStream_.Position = (long)offset64; long sig64 = ReadLEUint(); if( sig64 != BlubbZipConstants.Blubb64CentralFileHeaderSignature ) { throw new BlubbZipException( string.Format( "Invalid Blubb64 Central directory signature at {0:X}", offset64 ) ); } // NOTE: Record size = SizeOfFixedFields + SizeOfVariableData - 12. ulong recordSize = (ulong)ReadLEUlong(); int versionMadeBy = ReadLEUshort(); int versionToExtract = ReadLEUshort(); uint thisDisk = ReadLEUint(); uint centralDirDisk = ReadLEUint(); entriesForThisDisk = ReadLEUlong(); entriesForWholeCentralDir = ReadLEUlong(); centralDirSize = ReadLEUlong(); offsetOfCentralDir = (long)ReadLEUlong(); // NOTE: blubb64 extensible data sector (variable size) is ignored. } entries_ = new BlubbZipEntry[ entriesForThisDisk ]; // SFX/embedded support, find the offset of the first entry vis the start of the stream // This applies to Blubb files that are appended to the end of an SFX stub. // Or are appended as a resource to an executable. // Blubb files created by some archivers have the offsets altered to reflect the true offsets // and so dont require any adjustment here... // TODO: Difficulty with Blubb64 and SFX offset handling needs resolution - maths? if( !isBlubb64 && ( offsetOfCentralDir < locatedEndOfCentralDir - ( 4 + (long)centralDirSize ) ) ) { offsetOfFirstEntry = locatedEndOfCentralDir - ( 4 + (long)centralDirSize + offsetOfCentralDir ); if( offsetOfFirstEntry <= 0 ) { throw new BlubbZipException( "Invalid embedded blubb archive" ); } } baseStream_.Seek( offsetOfFirstEntry + offsetOfCentralDir, SeekOrigin.Begin ); for( ulong i = 0; i < entriesForThisDisk; i++ ) { if( ReadLEUint() != BlubbZipConstants.CentralHeaderSignature ) { throw new BlubbZipException( "Wrong Central Directory signature" ); } int versionMadeBy = ReadLEUshort(); int versionToExtract = ReadLEUshort(); int bitFlags = ReadLEUshort(); int method = ReadLEUshort(); uint dostime = ReadLEUint(); uint crc = ReadLEUint(); long csize = (long)ReadLEUint(); long size = (long)ReadLEUint(); int nameLen = ReadLEUshort(); int extraLen = ReadLEUshort(); int commentLen = ReadLEUshort(); int diskStartNo = ReadLEUshort(); // Not currently used int internalAttributes = ReadLEUshort(); // Not currently used uint externalAttributes = ReadLEUint(); long offset = ReadLEUint(); byte[] buffer = new byte[ Math.Max( nameLen, commentLen ) ]; StreamUtils.ReadFully( baseStream_, buffer, 0, nameLen ); string name = BlubbZipConstants.ConvertToStringExt( bitFlags, buffer, nameLen ); BlubbZipEntry entry = new BlubbZipEntry( name, versionToExtract, versionMadeBy, (CompressionMethod)method ); entry.Crc = crc & 0xffffffffL; entry.Size = size & 0xffffffffL; entry.CompressedSize = csize & 0xffffffffL; entry.Flags = bitFlags; entry.DosTime = (uint)dostime; entry.BlubbFileIndex = (long)i; entry.Offset = offset; entry.ExternalFileAttributes = (int)externalAttributes; if( ( bitFlags & 8 ) == 0 ) { entry.CryptoCheckValue = (byte)( crc >> 24 ); } else { entry.CryptoCheckValue = (byte)( ( dostime >> 8 ) & 0xff ); } if( extraLen > 0 ) { byte[] extra = new byte[ extraLen ]; StreamUtils.ReadFully( baseStream_, extra ); entry.ExtraData = extra; } entry.ProcessExtraData( false ); if( commentLen > 0 ) { StreamUtils.ReadFully( baseStream_, buffer, 0, commentLen ); entry.Comment = BlubbZipConstants.ConvertToStringExt( bitFlags, buffer, commentLen ); } entries_[ i ] = entry; } }
/// <summary> /// Get an output stream for the specified <see cref="BlubbEntry"/> /// </summary> /// <param name="entry">The entry to get an output stream for.</param> /// <returns>The output stream obtained for the entry.</returns> Stream GetOutputStream( BlubbZipEntry entry ) { Stream result = baseStream_; if( entry.IsCrypted == true ) { result = CreateAndInitEncryptionStream( result, entry ); } switch( entry.CompressionMethod ) { case CompressionMethod.Stored: result = new UncompressedStream( result ); break; case CompressionMethod.Deflated: DeflaterOutputStream dos = new DeflaterOutputStream( result, new Deflater( 9, true ) ); dos.IsStreamOwner = false; result = dos; break; default: throw new BlubbZipException( "Unknown compression method " + entry.CompressionMethod ); } return result; }
Stream CreateAndInitDecryptionStream( Stream baseStream, BlubbZipEntry entry ) { CryptoStream result = null; if( ( entry.Version < BlubbZipConstants.VersionStrongEncryption ) || ( entry.Flags & (int)GeneralBitFlags.StrongEncryption ) == 0 ) { PkblubbClassicManaged classicManaged = new PkblubbClassicManaged(); OnKeysRequired( entry.Name ); if( HaveKeys == false ) { throw new BlubbZipException( "No password available for encrypted stream" ); } result = new CryptoStream( baseStream, classicManaged.CreateDecryptor( key, null ), CryptoStreamMode.Read ); CheckClassicPassword( result, entry ); } else { throw new BlubbZipException( "Decryption method not supported" ); } return result; }
public BlubbUpdate( string fileName, string entryName, CompressionMethod compressionMethod ) { command_ = UpdateCommand.Add; entry_ = new BlubbZipEntry( entryName ); entry_.CompressionMethod = compressionMethod; filename_ = fileName; }
static void CheckClassicPassword( CryptoStream classicCryptoStream, BlubbZipEntry entry ) { byte[] cryptbuffer = new byte[ BlubbZipConstants.CryptoHeaderSize ]; StreamUtils.ReadFully( classicCryptoStream, cryptbuffer ); if( cryptbuffer[ BlubbZipConstants.CryptoHeaderSize - 1 ] != entry.CryptoCheckValue ) { throw new BlubbZipException( "Invalid password" ); } }
public BlubbUpdate( IStaticDataSource dataSource, BlubbZipEntry entry ) { command_ = UpdateCommand.Add; entry_ = entry; dataSource_ = dataSource; }
// IDataSource Members /// <summary> /// Get a <see cref="Stream"/> providing data for an entry. /// </summary> /// <param name="entry">The entry to provide data for.</param> /// <param name="name">The file name for data if known.</param> /// <returns>Returns a stream providing data; or null if not available</returns> public Stream GetSource( BlubbZipEntry entry, string name ) { Stream result = null; if( name != null ) { result = File.OpenRead( name ); } return result; }
public BlubbUpdate( UpdateCommand command, BlubbZipEntry entry ) { command_ = command; entry_ = (BlubbZipEntry)entry.Clone(); }
/// <summary> /// Write a data descriptor. /// </summary> /// <param name="entry">The entry to write a descriptor for.</param> /// <returns>Returns the number of descriptor bytes written.</returns> public int WriteDataDescriptor( BlubbZipEntry entry ) { if( entry == null ) { throw new ArgumentNullException( "entry" ); } int result = 0; // Add data descriptor if flagged as required if( ( entry.Flags & (int)GeneralBitFlags.Descriptor ) != 0 ) { // The signature is not PKZIP originally but is now described as optional // in the PKZIP Appnote documenting trhe format. WriteLEInt( BlubbZipConstants.DataDescriptorSignature ); WriteLEInt( unchecked( (int)( entry.Crc ) ) ); result += 8; if( entry.LocalHeaderRequiresBlubb64 ) { WriteLELong( entry.CompressedSize ); WriteLELong( entry.Size ); result += 16; } else { WriteLEInt( (int)entry.CompressedSize ); WriteLEInt( (int)entry.Size ); result += 8; } } return result; }
void DisposeInternal( bool disposing ) { if( !isDisposed_ ) { isDisposed_ = true; entries_ = new BlubbZipEntry[ 0 ]; if( IsStreamOwner && ( baseStream_ != null ) ) { lock( baseStream_ ) { baseStream_.Close(); } } PostUpdateCleanup(); } }
void ExtractFileEntry(BlubbZipEntry entry, string targetName) { bool proceed = true; if (overwrite_ != Overwrite.Always) { if (File.Exists(targetName)) { if ((overwrite_ == Overwrite.Prompt) && (confirmDelegate_ != null)) { proceed = confirmDelegate_(targetName); } else { proceed = false; } } } if (proceed) { if (events_ != null) { continueRunning_ = events_.OnProcessFile(entry.Name); } if (continueRunning_) { try { using (FileStream outputStream = File.Create(targetName)) { if (buffer_ == null) { buffer_ = new byte[4096]; } if ((events_ != null) && (events_.Progress != null)) { StreamUtils.Copy(blubbFile_.GetInputStream(entry), outputStream, buffer_, events_.Progress, events_.ProgressInterval, this, entry.Name, entry.Size); } else { StreamUtils.Copy(blubbFile_.GetInputStream(entry), outputStream, buffer_); } if (events_ != null) { continueRunning_ = events_.OnCompletedFile(entry.Name); } } #if !NETCF_1_0 && !NETCF_2_0 if (restoreDateTimeOnExtract_) { File.SetLastWriteTime(targetName, entry.DateTime); } if (RestoreAttributesOnExtract && entry.IsDOSEntry && (entry.ExternalFileAttributes != -1)) { FileAttributes fileAttributes = (FileAttributes)entry.ExternalFileAttributes; // TODO: SimpleBlubb - Setting of other file attributes on extraction is a little trickier. fileAttributes &= (FileAttributes.Archive | FileAttributes.Normal | FileAttributes.ReadOnly | FileAttributes.Hidden); File.SetAttributes(targetName, fileAttributes); } #endif } catch (Exception ex) { if (events_ != null) { continueRunning_ = events_.OnFileFailure(targetName, ex); } else { continueRunning_ = false; throw; } } } } }
/// <summary> /// Locate the data for a given entry. /// </summary> /// <returns> /// The start offset of the data. /// </returns> /// <exception cref="System.IO.EndOfStreamException"> /// The stream ends prematurely /// </exception> /// <exception cref="GodLesZ.Library.BlubbZip.Blubb.BlubbException"> /// The local header signature is invalid, the entry and central header file name lengths are different /// or the local and entry compression methods dont match /// </exception> long LocateEntry( BlubbZipEntry entry ) { return TestLocalHeader( entry, HeaderTest.Extract ); }
void ExtractEntry(BlubbZipEntry entry) { bool doExtraction = entry.IsCompressionMethodSupported(); string targetName = entry.Name; if (doExtraction) { if (entry.IsFile) { targetName = extractNameTransform_.TransformFile(targetName); } else if (entry.IsDirectory) { targetName = extractNameTransform_.TransformDirectory(targetName); } doExtraction = !((targetName == null) || (targetName.Length == 0)); } // TODO: Fire delegate/throw exception were compression method not supported, or name is invalid? string dirName = null; if (doExtraction) { if (entry.IsDirectory) { dirName = targetName; } else { dirName = Path.GetDirectoryName(Path.GetFullPath(targetName)); } } if (doExtraction && !Directory.Exists(dirName)) { if (!entry.IsDirectory || CreateEmptyDirectories) { try { Directory.CreateDirectory(dirName); } catch (Exception ex) { doExtraction = false; if (events_ != null) { if (entry.IsDirectory) { continueRunning_ = events_.OnDirectoryFailure(targetName, ex); } else { continueRunning_ = events_.OnFileFailure(targetName, ex); } } else { continueRunning_ = false; throw; } } } } if (doExtraction && entry.IsFile) { ExtractFileEntry(entry, targetName); } }
Stream CreateAndInitEncryptionStream( Stream baseStream, BlubbZipEntry entry ) { CryptoStream result = null; if( ( entry.Version < BlubbZipConstants.VersionStrongEncryption ) || ( entry.Flags & (int)GeneralBitFlags.StrongEncryption ) == 0 ) { PkblubbClassicManaged classicManaged = new PkblubbClassicManaged(); OnKeysRequired( entry.Name ); if( HaveKeys == false ) { throw new BlubbZipException( "No password available for encrypted stream" ); } // Closing a CryptoStream will close the base stream as well so wrap it in an UncompressedStream // which doesnt do this. result = new CryptoStream( new UncompressedStream( baseStream ), classicManaged.CreateEncryptor( key, null ), CryptoStreamMode.Write ); if( ( entry.Crc < 0 ) || ( entry.Flags & 8 ) != 0 ) { WriteEncryptionHeader( result, entry.DosTime << 16 ); } else { WriteEncryptionHeader( result, entry.Crc ); } } return result; }
/// <summary> /// Add a <see cref="BlubbEntry"/> that contains no data. /// </summary> /// <param name="entry">The entry to add.</param> /// <remarks>This can be used to add directories, volume labels, or empty file entries.</remarks> public void Add( BlubbZipEntry entry ) { if( entry == null ) { throw new ArgumentNullException( "entry" ); } CheckUpdating(); if( ( entry.Size != 0 ) || ( entry.CompressedSize != 0 ) ) { throw new BlubbZipException( "Entry cannot have any data" ); } AddUpdate( new BlubbUpdate( UpdateCommand.Add, entry ) ); }
public BlubbEntryEnumerator( BlubbZipEntry[] entries ) { mEntrys = entries; }
/// <summary> /// Delete a <see cref="BlubbEntry"/> from the archive. /// </summary> /// <param name="entry">The entry to delete.</param> public void Delete( BlubbZipEntry entry ) { if( entry == null ) { throw new ArgumentNullException( "entry" ); } CheckUpdating(); int index = FindExistingUpdate( entry ); if( index >= 0 ) { contentsEdited_ = true; updates_[ index ] = null; updateCount_ -= 1; } else { throw new BlubbZipException( "Cannot find entry to delete" ); } }
/// <summary> /// Opens a Blubb file reading the given <see cref="Stream"/>. /// </summary> /// <param name="stream">The <see cref="Stream"/> to read archive data from.</param> /// <exception cref="IOException"> /// An i/o error occurs /// </exception> /// <exception cref="BlubbException"> /// The stream doesn't contain a valid blubb archive.<br/> /// </exception> /// <exception cref="ArgumentException"> /// The <see cref="Stream">stream</see> doesnt support seeking. /// </exception> /// <exception cref="ArgumentNullException"> /// The <see cref="Stream">stream</see> argument is null. /// </exception> public BlubbZipFile( Stream stream ) { if( stream == null ) { throw new ArgumentNullException( "stream" ); } if( !stream.CanSeek ) { throw new ArgumentException( "Stream is not seekable", "stream" ); } baseStream_ = stream; isStreamOwner = true; if( baseStream_.Length > 0 ) { try { ReadEntries(); } catch { DisposeInternal( true ); throw; } } else { entries_ = new BlubbZipEntry[ 0 ]; isNewArchive_ = true; } }
int WriteCentralDirectoryHeader( BlubbZipEntry entry ) { if( entry.CompressedSize < 0 ) { throw new BlubbZipException( "Attempt to write central directory entry with unknown csize" ); } if( entry.Size < 0 ) { throw new BlubbZipException( "Attempt to write central directory entry with unknown size" ); } if( entry.Crc < 0 ) { throw new BlubbZipException( "Attempt to write central directory entry with unknown crc" ); } // Write the central file header WriteLEInt( BlubbZipConstants.CentralHeaderSignature ); // Version made by WriteLEShort( BlubbZipConstants.VersionMadeBy ); // Version required to extract WriteLEShort( entry.Version ); WriteLEShort( entry.Flags ); unchecked { WriteLEShort( (byte)entry.CompressionMethod ); WriteLEInt( (int)entry.DosTime ); WriteLEInt( (int)entry.Crc ); } if( ( entry.IsBlubb64Forced() ) || ( entry.CompressedSize >= 0xffffffff ) ) { WriteLEInt( -1 ); } else { WriteLEInt( (int)( entry.CompressedSize & 0xffffffff ) ); } if( ( entry.IsBlubb64Forced() ) || ( entry.Size >= 0xffffffff ) ) { WriteLEInt( -1 ); } else { WriteLEInt( (int)entry.Size ); } byte[] name = BlubbZipConstants.ConvertToArray( entry.Flags, entry.Name ); if( name.Length > 0xFFFF ) { throw new BlubbZipException( "Entry name is too long." ); } WriteLEShort( name.Length ); // Central header extra data is different to local header version so regenerate. BlubbZipExtraData ed = new BlubbZipExtraData( entry.ExtraData ); if( entry.CentralHeaderRequiresBlubb64 ) { ed.StartNewEntry(); if( ( entry.Size >= 0xffffffff ) || ( useBlubb64_ == UseBlubb64.On ) ) { ed.AddLeLong( entry.Size ); } if( ( entry.CompressedSize >= 0xffffffff ) || ( useBlubb64_ == UseBlubb64.On ) ) { ed.AddLeLong( entry.CompressedSize ); } if( entry.Offset >= 0xffffffff ) { ed.AddLeLong( entry.Offset ); } // Number of disk on which this file starts isnt supported and is never written here. ed.AddNewEntry( 1 ); } else { // Should have already be done when local header was added. ed.Delete( 1 ); } byte[] centralExtraData = ed.GetEntryData(); WriteLEShort( centralExtraData.Length ); WriteLEShort( entry.Comment != null ? entry.Comment.Length : 0 ); WriteLEShort( 0 ); // disk number WriteLEShort( 0 ); // internal file attributes // External file attributes... if( entry.ExternalFileAttributes != -1 ) { WriteLEInt( entry.ExternalFileAttributes ); } else { if( entry.IsDirectory ) { WriteLEUint( 16 ); } else { WriteLEUint( 0 ); } } if( entry.Offset >= 0xffffffff ) { WriteLEUint( 0xffffffff ); } else { WriteLEUint( (uint)(int)entry.Offset ); } if( name.Length > 0 ) { baseStream_.Write( name, 0, name.Length ); } if( centralExtraData.Length > 0 ) { baseStream_.Write( centralExtraData, 0, centralExtraData.Length ); } byte[] rawComment = ( entry.Comment != null ) ? Encoding.ASCII.GetBytes( entry.Comment ) : new byte[ 0 ]; if( rawComment.Length > 0 ) { baseStream_.Write( rawComment, 0, rawComment.Length ); } return BlubbZipConstants.CentralHeaderBaseSize + name.Length + centralExtraData.Length + rawComment.Length; }
/// <summary> /// Gets an input stream for reading the given blubb entry data in an uncompressed form. /// Normally the <see cref="BlubbEntry"/> should be an entry returned by GetEntry(). /// </summary> /// <param name="entry">The <see cref="BlubbEntry"/> to obtain a data <see cref="Stream"/> for</param> /// <returns>An input <see cref="Stream"/> containing data for this <see cref="BlubbEntry"/></returns> /// <exception cref="ObjectDisposedException"> /// The BlubbFile has already been closed /// </exception> /// <exception cref="GodLesZ.Library.BlubbZip.Blubb.BlubbException"> /// The compression method for the entry is unknown /// </exception> /// <exception cref="IndexOutOfRangeException"> /// The entry is not found in the BlubbFile /// </exception> public Stream GetInputStream( BlubbZipEntry entry ) { if( entry == null ) { throw new ArgumentNullException( "entry" ); } if( isDisposed_ ) { throw new ObjectDisposedException( "BlubbFile" ); } long index = entry.BlubbFileIndex; if( ( index < 0 ) || ( index >= entries_.Length ) || ( entries_[ index ].Name != entry.Name ) ) { index = FindEntry( entry.Name, true ); if( index < 0 ) { throw new BlubbZipException( "Entry cannot be found" ); } } return GetInputStream( index ); }
internal void SetEntry( BlubbZipEntry entry ) { entry_ = entry; entryValid_ = true; bytesTested_ = 0; }
public static void ZipStream(string sourceFolder, string fileName, Stream streamData, BlubbZipOutputStream outputStream) { if (streamData.CanRead == false || streamData.CanSeek == false) return; byte[] buffer = new byte[65535]; int readCount; BlubbZipEntry entry = new BlubbZipEntry(BlubbZipEntry.CleanName(Tools.PatchKnownProblems(Tools.MakePathRelative(sourceFolder, fileName)))); streamData.Seek(0, SeekOrigin.Begin); entry.DateTime = DateTime.Now; entry.Size = streamData.Length; outputStream.PutNextEntry(entry); crc.Reset(); while ((readCount = streamData.Read(buffer, 0, buffer.Length)) > 0) { crc.Update(buffer, 0, readCount); outputStream.Write(buffer, 0, readCount); } entry.Crc = crc.Value; }
// Write the local file header // TODO: BlubbHelperStream.WriteLocalHeader is not yet used and needs checking for BlubbFile and BlubbOuptutStream usage void WriteLocalHeader(BlubbZipEntry entry, EntryPatchData patchData) { CompressionMethod method = entry.CompressionMethod; bool headerInfoAvailable = true; // How to get this? bool patchEntryHeader = false; WriteLEInt(BlubbZipConstants.LocalHeaderSignature); WriteLEShort(entry.Version); WriteLEShort(entry.Flags); WriteLEShort((byte)method); WriteLEInt((int)entry.DosTime); if (headerInfoAvailable == true) { WriteLEInt((int)entry.Crc); if (entry.LocalHeaderRequiresBlubb64) { WriteLEInt(-1); WriteLEInt(-1); } else { WriteLEInt(entry.IsCrypted ? (int)entry.CompressedSize + BlubbZipConstants.CryptoHeaderSize : (int)entry.CompressedSize); WriteLEInt((int)entry.Size); } } else { if (patchData != null) { patchData.CrcPatchOffset = stream_.Position; } WriteLEInt(0); // Crc if (patchData != null) { patchData.SizePatchOffset = stream_.Position; } // For local header both sizes appear in Blubb64 Extended Information if (entry.LocalHeaderRequiresBlubb64 && patchEntryHeader) { WriteLEInt(-1); WriteLEInt(-1); } else { WriteLEInt(0); // Compressed size WriteLEInt(0); // Uncompressed size } } byte[] name = BlubbZipConstants.ConvertToArray(entry.Flags, entry.Name); if (name.Length > 0xFFFF) { throw new BlubbZipException("Entry name too long."); } BlubbZipExtraData ed = new BlubbZipExtraData(entry.ExtraData); if (entry.LocalHeaderRequiresBlubb64 && (headerInfoAvailable || patchEntryHeader)) { ed.StartNewEntry(); if (headerInfoAvailable) { ed.AddLeLong(entry.Size); ed.AddLeLong(entry.CompressedSize); } else { ed.AddLeLong(-1); ed.AddLeLong(-1); } ed.AddNewEntry(1); if (!ed.Find(1)) { throw new BlubbZipException("Internal error cant find extra data"); } if (patchData != null) { patchData.SizePatchOffset = ed.CurrentReadIndex; } } else { ed.Delete(1); } byte[] extra = ed.GetEntryData(); WriteLEShort(name.Length); WriteLEShort(extra.Length); if (name.Length > 0) { stream_.Write(name, 0, name.Length); } if (entry.LocalHeaderRequiresBlubb64 && patchEntryHeader) { patchData.SizePatchOffset += stream_.Position; } if (extra.Length > 0) { stream_.Write(extra, 0, extra.Length); } }