private void CreateFci(long maxArchiveSize) { NativeMethods.FCI.CCAB cCAB = new NativeMethods.FCI.CCAB(); checked { if (maxArchiveSize > 0 && maxArchiveSize < cCAB.cb) { cCAB.cb = Math.Max(32768, (int)maxArchiveSize); } object option = context.GetOption("maxFolderSize", null); if (option != null) { long num = Convert.ToInt64(option, CultureInfo.InvariantCulture); if (num > 0 && num < cCAB.cbFolderThresh) { cCAB.cbFolderThresh = (int)num; } } maxCabBytes = cCAB.cb; cCAB.szCab = context.GetArchiveName(0); if (cCAB.szCab == null) { throw new FileNotFoundException("Cabinet name not provided by stream context."); } cCAB.setID = (short)new Random().Next(-32768, 32768); base.CabNumbers[cCAB.szCab] = 0; currentArchiveName = cCAB.szCab; totalArchives = 1; base.CabStream = null; base.Erf.Clear(); fciHandle = NativeMethods.FCI.Create(base.ErfHandle.AddrOfPinnedObject(), fciFilePlacedHandler, fciAllocMemHandler, fciFreeMemHandler, fciOpenStreamHandler, fciReadStreamHandler, fciWriteStreamHandler, fciCloseStreamHandler, fciSeekStreamHandler, fciDeleteFileHandler, fciGetTempFileHandler, cCAB, IntPtr.Zero); CheckError(extracting: false); } }
private void CreateFci(long maxArchiveSize) { NativeMethods.FCI.CCAB ccab = new NativeMethods.FCI.CCAB(); if (maxArchiveSize > 0 && maxArchiveSize < ccab.cb) { ccab.cb = Math.Max( NativeMethods.FCI.MIN_DISK, (int)maxArchiveSize); } object maxFolderSizeOption = this.context.GetOption( "maxFolderSize", null); if (maxFolderSizeOption != null) { long maxFolderSize = Convert.ToInt64( maxFolderSizeOption, CultureInfo.InvariantCulture); if (maxFolderSize > 0 && maxFolderSize < ccab.cbFolderThresh) { ccab.cbFolderThresh = (int)maxFolderSize; } } this.maxCabBytes = ccab.cb; ccab.szCab = this.context.GetArchiveName(0); if (ccab.szCab == null) { throw new FileNotFoundException( "Cabinet name not provided by stream context."); } ccab.setID = (short)new Random().Next( Int16.MinValue, Int16.MaxValue + 1); this.CabNumbers[ccab.szCab] = 0; this.currentArchiveName = ccab.szCab; this.totalArchives = 1; this.CabStream = null; this.Erf.Clear(); this.fciHandle = NativeMethods.FCI.Create( this.ErfHandle.AddrOfPinnedObject(), this.fciFilePlacedHandler, this.fciAllocMemHandler, this.fciFreeMemHandler, this.fciOpenStreamHandler, this.fciReadStreamHandler, this.fciWriteStreamHandler, this.fciCloseStreamHandler, this.fciSeekStreamHandler, this.fciDeleteFileHandler, this.fciGetTempFileHandler, ccab, IntPtr.Zero); this.CheckError(false); }
protected override void Dispose(bool disposing) { try { if (disposing && fciHandle != null) { fciHandle.Dispose(); fciHandle = null; } } finally { base.Dispose(disposing); } }
protected override void Dispose(bool disposing) { try { if (disposing) { if (this.fciHandle != null) { this.fciHandle.Dispose(); this.fciHandle = null; } } } finally { base.Dispose(disposing); } }
public void Pack( IPackStreamContext streamContext, IEnumerable<string> files, long maxArchiveSize) { if (streamContext == null) { throw new ArgumentNullException("streamContext"); } if (files == null) { throw new ArgumentNullException("files"); } lock (this) { try { this.context = streamContext; this.ResetProgressData(); this.CreateFci(maxArchiveSize); foreach (string file in files) { FileAttributes attributes; DateTime lastWriteTime; Stream fileStream = this.context.OpenFileReadStream( file, out attributes, out lastWriteTime); if (fileStream != null) { this.totalFileBytes += fileStream.Length; this.totalFiles++; this.context.CloseFileReadStream(file, fileStream); } } long uncompressedBytesInFolder = 0; this.currentFileNumber = -1; foreach (string file in files) { FileAttributes attributes; DateTime lastWriteTime; Stream fileStream = this.context.OpenFileReadStream( file, out attributes, out lastWriteTime); if (fileStream == null) { continue; } if (fileStream.Length >= (long) NativeMethods.FCI.MAX_FOLDER) { throw new NotSupportedException(String.Format( CultureInfo.InvariantCulture, "File {0} exceeds maximum file size " + "for cabinet format.", file)); } if (uncompressedBytesInFolder > 0) { // Automatically create a new folder if this file // won't fit in the current folder. bool nextFolder = uncompressedBytesInFolder + fileStream.Length >= (long) NativeMethods.FCI.MAX_FOLDER; // Otherwise ask the client if it wants to // move to the next folder. if (!nextFolder) { object nextFolderOption = streamContext.GetOption( "nextFolder", new object[] { file, this.currentFolderNumber }); nextFolder = Convert.ToBoolean( nextFolderOption, CultureInfo.InvariantCulture); } if (nextFolder) { this.FlushFolder(); uncompressedBytesInFolder = 0; } } if (this.currentFolderTotalBytes > 0) { this.currentFolderTotalBytes = 0; this.currentFolderNumber++; uncompressedBytesInFolder = 0; } this.currentFileName = file; this.currentFileNumber++; this.currentFileTotalBytes = fileStream.Length; this.currentFileBytesProcessed = 0; this.OnProgress(ArchiveProgressType.StartFile); uncompressedBytesInFolder += fileStream.Length; this.AddFile( file, fileStream, attributes, lastWriteTime, false, this.CompressionLevel); } this.FlushFolder(); this.FlushCabinet(); } finally { if (this.CabStream != null) { this.context.CloseArchiveWriteStream( this.currentArchiveNumber, this.currentArchiveName, this.CabStream); this.CabStream = null; } if (this.FileStream != null) { this.context.CloseFileReadStream( this.currentFileName, this.FileStream); this.FileStream = null; } this.context = null; if (this.fciHandle != null) { this.fciHandle.Dispose(); this.fciHandle = null; } } } }
private void CreateFci(long maxArchiveSize) { NativeMethods.FCI.CCAB ccab = new NativeMethods.FCI.CCAB(); if (maxArchiveSize > 0 && maxArchiveSize < ccab.cb) { ccab.cb = Math.Max( NativeMethods.FCI.MIN_DISK, (int) maxArchiveSize); } object maxFolderSizeOption = this.context.GetOption( "maxFolderSize", null); if (maxFolderSizeOption != null) { long maxFolderSize = Convert.ToInt64( maxFolderSizeOption, CultureInfo.InvariantCulture); if (maxFolderSize > 0 && maxFolderSize < ccab.cbFolderThresh) { ccab.cbFolderThresh = (int) maxFolderSize; } } this.maxCabBytes = ccab.cb; ccab.szCab = this.context.GetArchiveName(0); if (ccab.szCab == null) { throw new FileNotFoundException( "Cabinet name not provided by stream context."); } ccab.setID = (short) new Random().Next( Int16.MinValue, Int16.MaxValue + 1); this.CabNumbers[ccab.szCab] = 0; this.currentArchiveName = ccab.szCab; this.totalArchives = 1; this.CabStream = null; this.Erf.Clear(); this.fciHandle = NativeMethods.FCI.Create( this.ErfHandle.AddrOfPinnedObject(), this.fciFilePlacedHandler, this.fciAllocMemHandler, this.fciFreeMemHandler, this.fciOpenStreamHandler, this.fciReadStreamHandler, this.fciWriteStreamHandler, this.fciCloseStreamHandler, this.fciSeekStreamHandler, this.fciDeleteFileHandler, this.fciGetTempFileHandler, ccab, IntPtr.Zero); this.CheckError(false); }
public void Pack( IPackStreamContext streamContext, IEnumerable <string> files, long maxArchiveSize) { if (streamContext == null) { throw new ArgumentNullException("streamContext"); } if (files == null) { throw new ArgumentNullException("files"); } lock (this) { try { this.context = streamContext; this.ResetProgressData(); this.CreateFci(maxArchiveSize); foreach (string file in files) { FileAttributes attributes; DateTime lastWriteTime; Stream fileStream = this.context.OpenFileReadStream( file, out attributes, out lastWriteTime); if (fileStream != null) { this.totalFileBytes += fileStream.Length; this.totalFiles++; this.context.CloseFileReadStream(file, fileStream); } } long uncompressedBytesInFolder = 0; this.currentFileNumber = -1; foreach (string file in files) { FileAttributes attributes; DateTime lastWriteTime; Stream fileStream = this.context.OpenFileReadStream( file, out attributes, out lastWriteTime); if (fileStream == null) { continue; } if (fileStream.Length >= (long)NativeMethods.FCI.MAX_FOLDER) { throw new NotSupportedException(String.Format( CultureInfo.InvariantCulture, "File {0} exceeds maximum file size " + "for cabinet format.", file)); } if (uncompressedBytesInFolder > 0) { // Automatically create a new folder if this file // won't fit in the current folder. bool nextFolder = uncompressedBytesInFolder + fileStream.Length >= (long)NativeMethods.FCI.MAX_FOLDER; // Otherwise ask the client if it wants to // move to the next folder. if (!nextFolder) { object nextFolderOption = streamContext.GetOption( "nextFolder", new object[] { file, this.currentFolderNumber }); nextFolder = Convert.ToBoolean( nextFolderOption, CultureInfo.InvariantCulture); } if (nextFolder) { this.FlushFolder(); uncompressedBytesInFolder = 0; } } if (this.currentFolderTotalBytes > 0) { this.currentFolderTotalBytes = 0; this.currentFolderNumber++; uncompressedBytesInFolder = 0; } this.currentFileName = file; this.currentFileNumber++; this.currentFileTotalBytes = fileStream.Length; this.currentFileBytesProcessed = 0; this.OnProgress(ArchiveProgressType.StartFile); uncompressedBytesInFolder += fileStream.Length; this.AddFile( file, fileStream, attributes, lastWriteTime, false, this.CompressionLevel); } this.FlushFolder(); this.FlushCabinet(); } finally { if (this.CabStream != null) { this.context.CloseArchiveWriteStream( this.currentArchiveNumber, this.currentArchiveName, this.CabStream); this.CabStream = null; } if (this.FileStream != null) { this.context.CloseFileReadStream( this.currentFileName, this.FileStream); this.FileStream = null; } this.context = null; if (this.fciHandle != null) { this.fciHandle.Dispose(); this.fciHandle = null; } } } }
public void Pack(IPackStreamContext streamContext, IEnumerable <string> files, long maxArchiveSize) { if (streamContext == null) { throw new ArgumentNullException("streamContext"); } if (files == null) { throw new ArgumentNullException("files"); } checked { lock (this) { try { context = streamContext; ResetProgressData(); CreateFci(maxArchiveSize); foreach (string file in files) { FileAttributes attributes; DateTime lastWriteTime; Stream stream = context.OpenFileReadStream(file, out attributes, out lastWriteTime); if (stream != null) { totalFileBytes += stream.Length; totalFiles++; context.CloseFileReadStream(file, stream); } } long num = 0L; currentFileNumber = -1; foreach (string file2 in files) { FileAttributes attributes2; DateTime lastWriteTime2; Stream stream2 = context.OpenFileReadStream(file2, out attributes2, out lastWriteTime2); if (stream2 == null) { continue; } if (stream2.Length >= 2147450880) { throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, "File {0} exceeds maximum file size for cabinet format.", file2)); } if (num > 0) { bool flag = num + stream2.Length >= 2147450880; if (!flag) { flag = Convert.ToBoolean(streamContext.GetOption("nextFolder", new object[2] { file2, currentFolderNumber }), CultureInfo.InvariantCulture); } if (flag) { FlushFolder(); num = 0L; } } if (currentFolderTotalBytes > 0) { currentFolderTotalBytes = 0L; currentFolderNumber++; num = 0L; } currentFileName = file2; currentFileNumber++; currentFileTotalBytes = stream2.Length; currentFileBytesProcessed = 0L; OnProgress(ArchiveProgressType.StartFile); num += stream2.Length; AddFile(file2, stream2, attributes2, lastWriteTime2, execute: false, CompressionLevel); } FlushFolder(); FlushCabinet(); } finally { if (base.CabStream != null) { context.CloseArchiveWriteStream(currentArchiveNumber, currentArchiveName, base.CabStream); base.CabStream = null; } if (base.FileStream != null) { context.CloseFileReadStream(currentFileName, base.FileStream); base.FileStream = null; } context = null; if (fciHandle != null) { fciHandle.Dispose(); fciHandle = null; } } } } }