SevenZip/SevenZipCompressor.cs
/// <summary>
///   Modifies the existing archive (renames files or deletes them).
/// </summary>
/// <param name = "archiveName">The archive file name.</param>
/// <param name = "newFileNames">New file names. Null value to delete the corresponding index.</param>
/// <param name = "password">The archive password.</param>
Modifies the existing archive (renames files or deletes them).
Modifies the existing archive ( renames files or deletes them ) .
public void ModifyArchive(string archiveName, Dictionary<int, string> newFileNames, string password
#if CS4 
            = ""
#endif
            )
        {
            ClearExceptions();
            if (!SevenZipLibraryManager.ModifyCapable)
            {
                throw new SevenZipLibraryException("The specified 7zip native library does not support this method.");
            }
            if (!File.Exists(archiveName))
            {
                if (!ThrowException(null, new ArgumentException("The specified archive does not exist.", "archiveName")))
                {
                    return;
                }
            }
            if (newFileNames == null || newFileNames.Count == 0)
            {
                if (!ThrowException(null, new ArgumentException("Invalid new file names.", "newFileNames")))
                {
                    return;
                }
            }
            try
            {
                using (var extr = new SevenZipExtractor(archiveName))
                {
                    _updateData = new UpdateData();
                    var archiveData = new ArchiveFileInfo[extr.ArchiveFileData.Count];
                    extr.ArchiveFileData.CopyTo(archiveData, 0);
                    _updateData.ArchiveFileData = new List<ArchiveFileInfo>(archiveData);
                }
                _updateData.FileNamesToModify = newFileNames;
                _updateData.Mode = InternalCompressionMode.Modify;
            }
            catch (SevenZipException e)
            {
                if (!ThrowException(null, e))
                {
                    return;
                }
            }
            try
            {
                ISequentialOutStream sequentialArchiveStream;
                _compressingFilesOnDisk = true;
                using ((sequentialArchiveStream = GetOutStream(GetArchiveFileStream(archiveName))) as IDisposable)
                {
                    IInStream inArchiveStream;
                    _archiveName = archiveName;
                    using ((inArchiveStream = GetInStream()) as IDisposable)
                    {
                        IOutArchive outArchive;
                        SevenZipLibraryManager.LoadLibrary(
                            this, Formats.InForOutFormats[_archiveFormat]);
                        if ((outArchive = MakeOutArchive(inArchiveStream)) == null)
                        {
                            return;
                        }
                        using (ArchiveUpdateCallback auc = GetArchiveUpdateCallback(null, 0, password))
                        {
                            UInt32 deleteCount = 0;
                            if (_updateData.FileNamesToModify != null)
                            {
#if CS4 
                                deleteCount = (UInt32)_updateData.FileNamesToModify.Sum(
                                    pairDeleted => pairDeleted.Value == null ? 1 : 0);
#else
                                foreach (KeyValuePair<int, string> pairDeleted in _updateData.FileNamesToModify)
                                {
                                    if (pairDeleted.Value == null)
                                    {
                                        deleteCount++;
                                    }
                                }
#endif
                            }
                            try
                            {
                                CheckedExecute(
                                    outArchive.UpdateItems(
                                        sequentialArchiveStream, _oldFilesCount - deleteCount, auc),
                                    SevenZipCompressionFailedException.DEFAULT_MESSAGE, auc);
                            }
                            finally
                            {
                                FreeCompressionCallback(auc);
                            }
                        }
                    }
                }
            }
            finally
            {
                SevenZipLibraryManager.FreeLibrary(this, Formats.InForOutFormats[_archiveFormat]);
                File.Delete(archiveName);
                FinalizeUpdate();
                _compressingFilesOnDisk = false;
                _updateData.FileNamesToModify = null;
                _updateData.ArchiveFileData = null;
                OnEvent(CompressionFinished, EventArgs.Empty, false);
            }
            ThrowUserException();
        }
public void ModifyArchive ( string archiveName , Dictionary < int , string > newFileNames , string password if CS4 = " " endif ) { ClearExceptions ( ) ; if ( ! SevenZipLibraryManager . ModifyCapable ) { throw new SevenZipLibraryException ( " " ) ; } if ( ! File . Exists ( archiveName ) ) { if ( ! ThrowException ( null , new ArgumentException ( " " , " " ) ) ) { return ; } } if ( newFileNames == null || newFileNames . Count == 0 ) { if ( ! ThrowException ( null , new ArgumentException ( " " , " " ) ) ) { return ; } } try { using ( var extr = new SevenZipExtractor ( archiveName ) ) { _updateData = new UpdateData ( ) ; var archiveData = new ArchiveFileInfo [ extr . ArchiveFileData . Count ] ; extr . ArchiveFileData . CopyTo ( archiveData , 0 ) ; _updateData . ArchiveFileData = new List < ArchiveFileInfo > ( archiveData ) ; } _updateData . FileNamesToModify = newFileNames ; _updateData . Mode = InternalCompressionMode . Modify ; } catch ( SevenZipException e ) { if ( ! ThrowException ( null , e ) ) { return ; } } try { ISequentialOutStream sequentialArchiveStream ; _compressingFilesOnDisk = true ; using ( ( sequentialArchiveStream = GetOutStream ( GetArchiveFileStream ( archiveName ) ) ) as IDisposable ) { IInStream inArchiveStream ; _archiveName = archiveName ; using ( ( inArchiveStream = GetInStream ( ) ) as IDisposable ) { IOutArchive outArchive ; SevenZipLibraryManager . LoadLibrary ( this , Formats . InForOutFormats [ _archiveFormat ] ) ; if ( ( outArchive = MakeOutArchive ( inArchiveStream ) ) == null ) { return ; } using ( ArchiveUpdateCallback auc = GetArchiveUpdateCallback ( null , 0 , password ) ) { UInt32 deleteCount = 0 ; if ( _updateData . FileNamesToModify != null ) { if CS4 deleteCount = ( UInt32 ) _updateData . FileNamesToModify . Sum ( pairDeleted => pairDeleted . Value == null ? 1 : 0 ) ; else foreach ( KeyValuePair < int , string > pairDeleted in _updateData . FileNamesToModify ) { if ( pairDeleted . Value == null ) { deleteCount ++ ; } } endif } try { CheckedExecute ( outArchive . UpdateItems ( sequentialArchiveStream , _oldFilesCount - deleteCount , auc ) , SevenZipCompressionFailedException . DEFAULT_MESSAGE , auc ) ; } finally { FreeCompressionCallback ( auc ) ; } } } } } finally { SevenZipLibraryManager . FreeLibrary ( this , Formats . InForOutFormats [ _archiveFormat ] ) ; File . Delete ( archiveName ) ; FinalizeUpdate ( ) ; _compressingFilesOnDisk = false ; _updateData . FileNamesToModify = null ; _updateData . ArchiveFileData = null ; OnEvent ( CompressionFinished , EventArgs . Empty , false ) ; } ThrowUserException ( ) ; }
Modifies the existing archive (renames files or deletes them).
Modifies the existing archive ( renames files or deletes them ) .
// Create IInArchive, read it and convert to IOutArchive // System.Linq of C# 4 is great
archiveName
string
The archive file name.
The archive file name .
newFileNames
Dictionary<int, string>
New file names. Null value to delete the corresponding index.
New file names . Null value to delete the corresponding index .
password
string
The archive password.
The archive password .