SevenZip/SevenZipCompressor.cs
/// <summary>
///   Packs files into the archive.
/// </summary>
/// <param name = "fileFullNames">Array of file names to pack.</param>
/// <param name = "commonRootLength">The length of the common root of the file names.</param>
/// <param name = "archiveStream">The archive output stream.
///   Use CompressFiles( ... string archiveName ... ) overloads for archiving to disk.</param>
/// <param name = "password">The archive password.</param>
Packs files into the archive.
Packs files into the archive .
public void CompressFilesEncrypted(
            Stream archiveStream, int commonRootLength, string password, params string[] fileFullNames)
        {
            ClearExceptions();
            if (fileFullNames.Length > 1 &&
                (_archiveFormat == OutArchiveFormat.BZip2 || _archiveFormat == OutArchiveFormat.GZip ||
                 _archiveFormat == OutArchiveFormat.XZ))
            {
                if (!ThrowException(null,
                                    new CompressionFailedException("Can not compress more than one file in this format.")))
                {
                    return;
                }
            }
            if (_volumeSize == 0 || !_compressingFilesOnDisk)
            {
                ValidateStream(archiveStream);
            }
            FileInfo[] files = null;
            try
            {
                files = ProduceFileInfoArray(fileFullNames, commonRootLength, _directoryCompress, DirectoryStructure);
            }
            catch (Exception e)
            {
                if (!ThrowException(null, e))
                {
                    return;
                }
            }
            _directoryCompress = false;
            if (FilesFound != null)
            {
                FilesFound(this, new IntEventArgs(fileFullNames.Length));
            }
            try
            {
                ISequentialOutStream sequentialArchiveStream;
                using ((sequentialArchiveStream = GetOutStream(archiveStream)) as IDisposable)
                {
                    IInStream inArchiveStream;
                    using ((inArchiveStream = GetInStream()) as IDisposable)
                    {
                        IOutArchive outArchive;
                        if (CompressionMode == CompressionMode.Create || !_compressingFilesOnDisk)
                        {
                            SevenZipLibraryManager.LoadLibrary(this, _archiveFormat);
                            outArchive = SevenZipLibraryManager.OutArchive(_archiveFormat, this);
                        }
                        else
                        {
                            SevenZipLibraryManager.LoadLibrary(
                                this, Formats.InForOutFormats[_archiveFormat]);
                            if ((outArchive = MakeOutArchive(inArchiveStream)) == null)
                            {
                                return;
                            }
                        }
                        using (ArchiveUpdateCallback auc = GetArchiveUpdateCallback(files, commonRootLength, password))
                        {
                            try
                            {
                                if (files != null) 
                                    CheckedExecute(
                                        outArchive.UpdateItems(
                                            sequentialArchiveStream, (uint) files.Length + _oldFilesCount, auc),
                                        SevenZipCompressionFailedException.DEFAULT_MESSAGE, auc);
                            }
                            finally
                            {
                                FreeCompressionCallback(auc);
                            }
                        }
                    }
                }
            }
            finally
            {
                if (CompressionMode == CompressionMode.Create || !_compressingFilesOnDisk)
                {
                    SevenZipLibraryManager.FreeLibrary(this, _archiveFormat);
                }
                else
                {
                    SevenZipLibraryManager.FreeLibrary(this, Formats.InForOutFormats[_archiveFormat]);
                    File.Delete(_archiveName);
                }
                _compressingFilesOnDisk = false;
                OnEvent(CompressionFinished, EventArgs.Empty, false);
            }
            ThrowUserException();
        }
public void CompressFilesEncrypted ( Stream archiveStream , int commonRootLength , string password , params string [ ] fileFullNames ) { ClearExceptions ( ) ; if ( fileFullNames . Length > 1 && ( _archiveFormat == OutArchiveFormat . BZip2 || _archiveFormat == OutArchiveFormat . GZip || _archiveFormat == OutArchiveFormat . XZ ) ) { if ( ! ThrowException ( null , new CompressionFailedException ( " " ) ) ) { return ; } } if ( _volumeSize == 0 || ! _compressingFilesOnDisk ) { ValidateStream ( archiveStream ) ; } FileInfo [ ] files = null ; try { files = ProduceFileInfoArray ( fileFullNames , commonRootLength , _directoryCompress , DirectoryStructure ) ; } catch ( Exception e ) { if ( ! ThrowException ( null , e ) ) { return ; } } _directoryCompress = false ; if ( FilesFound != null ) { FilesFound ( this , new IntEventArgs ( fileFullNames . Length ) ) ; } try { ISequentialOutStream sequentialArchiveStream ; using ( ( sequentialArchiveStream = GetOutStream ( archiveStream ) ) as IDisposable ) { IInStream inArchiveStream ; using ( ( inArchiveStream = GetInStream ( ) ) as IDisposable ) { IOutArchive outArchive ; if ( CompressionMode == CompressionMode . Create || ! _compressingFilesOnDisk ) { SevenZipLibraryManager . LoadLibrary ( this , _archiveFormat ) ; outArchive = SevenZipLibraryManager . OutArchive ( _archiveFormat , this ) ; } else { SevenZipLibraryManager . LoadLibrary ( this , Formats . InForOutFormats [ _archiveFormat ] ) ; if ( ( outArchive = MakeOutArchive ( inArchiveStream ) ) == null ) { return ; } } using ( ArchiveUpdateCallback auc = GetArchiveUpdateCallback ( files , commonRootLength , password ) ) { try { if ( files != null ) CheckedExecute ( outArchive . UpdateItems ( sequentialArchiveStream , ( uint ) files . Length + _oldFilesCount , auc ) , SevenZipCompressionFailedException . DEFAULT_MESSAGE , auc ) ; } finally { FreeCompressionCallback ( auc ) ; } } } } } finally { if ( CompressionMode == CompressionMode . Create || ! _compressingFilesOnDisk ) { SevenZipLibraryManager . FreeLibrary ( this , _archiveFormat ) ; } else { SevenZipLibraryManager . FreeLibrary ( this , Formats . InForOutFormats [ _archiveFormat ] ) ; File . Delete ( _archiveName ) ; } _compressingFilesOnDisk = false ; OnEvent ( CompressionFinished , EventArgs . Empty , false ) ; } ThrowUserException ( ) ; }
Packs files into the archive.
Packs files into the archive .
// Create IInArchive, read it and convert to IOutArchive //ReSharper
archiveStream
Stream
The archive output stream.
Use CompressFiles( ... string archiveName ... ) overloads for archiving to disk.
The archive output stream . Use CompressFiles ( ... string archiveName ... ) overloads for archiving to disk .
commonRootLength
int
The length of the common root of the file names.
The length of the common root of the file names .
password
string
The archive password.
The archive password .
fileFullNames
Array of file names to pack.
Array of file names to pack .