ZipArchive update mode

2 posts, 0 answers
  1. Chris
    Chris avatar
    5 posts
    Member since:
    Mar 2012

    Posted 10 Jun 2014 Link to this post

    I am working with some log files I generate with another application.  My intention is to archive them and then delete the large log file and have it stored in a compressed format for later access.    This works when I use ZipArchiveMode.Create.   Where this falls apart is that if I delete the file after I have added it to the archive, and then run the program again, it is not keeping the old entries in the archive - since I am using "Create".  I also wanted to save time that if the file I wanted to add was already in the archive, it would skip adding it.  A query against the Entries property of the ZipArchive does not seem to work when the mode is set to Create - it might work for the first time I ask, but once I add a file I get an error.  I then attempted to use ZipArchiveMode.Update, but then I received and ObjectDisposedException {"Cannot access a closed Stream."}.

    I am using the code found in this post
    to add the existing files to the archive.

    So I'm not sure if I am using the ZipArchive incorrectly, or I need to do it a different way, but help and guidance would be appreciated.  I can create a demo app if needed.

  2. Andrey
    Andrey avatar
    1681 posts

    Posted 11 Jun 2014 Link to this post

    Hello Chris,

    I think you can use ZipArchiveMode.Update if the archive file exists. Also you can check existing entries to skip them.
    The sample code is below.
    private void UpdateEncryptedZipFileFromDirectory(
        string sourceDirectoryName,
        string destinationArchiveFileName)
        char[] directorySeparatorChar = new char[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar };
        ZipArchiveMode archiveMode = File.Exists(destinationArchiveFileName) ? ZipArchiveMode.Update : ZipArchiveMode.Create;
        if (!string.IsNullOrEmpty(sourceDirectoryName))
            using (FileStream archiveStream = File.Open(
                // Set password to protect ZIP archive
                DefaultEncryptionSettings encryptionSettings = new DefaultEncryptionSettings();
                encryptionSettings.Password = "PaSsWoRd";
                using (ZipArchive archive = new ZipArchive(
                    foreach (string fileName in Directory.GetFiles(sourceDirectoryName))
                        int length = fileName.Length - sourceDirectoryName.Length;
                        string entryName = fileName.Substring(sourceDirectoryName.Length, length);
                        entryName = entryName.TrimStart(directorySeparatorChar);
                        if (archiveMode == ZipArchiveMode.Update && archive.GetEntry(entryName) != null)
                            // skip existing entry
                        using (FileStream file = File.OpenRead(fileName))
                            using (ZipArchiveEntry entry = archive.CreateEntry(entryName))
                                Stream entryStream = entry.Open();

    I hope this helps.

    Andrey Murzov
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
Back to Top