This is a migrated thread and some comments may be shown as answers.

ZipArchive update mode

1 Answer 174 Views
ZipLibrary
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 10 Jun 2014, 05:53 PM
Hi,
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
http://www.telerik.com/forums/zip-content-of-a-folder-into-an-encrypted-zip-file
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.

Thanks,
Chris

1 Answer, 1 is accepted

Sort by
0
Andrey
Telerik team
answered on 11 Jun 2014, 08:23 AM
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(
            destinationArchiveFileName,
            FileMode.OpenOrCreate))
        {
            // Set password to protect ZIP archive
            DefaultEncryptionSettings encryptionSettings = new DefaultEncryptionSettings();
            encryptionSettings.Password = "PaSsWoRd";
 
            using (ZipArchive archive = new ZipArchive(
                archiveStream,
                archiveMode,
                true,
                null,
                null,
                encryptionSettings))
            {
                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
                        continue;
                    }
 
                    using (FileStream file = File.OpenRead(fileName))
                    {
                        using (ZipArchiveEntry entry = archive.CreateEntry(entryName))
                        {
                            Stream entryStream = entry.Open();
                            file.CopyTo(entryStream);
                        }
                    }
                }
            }
        }
    }
}

I hope this helps.

Regards,
Andrey Murzov
Telerik
 
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.
 
Tags
ZipLibrary
Asked by
Chris
Top achievements
Rank 1
Answers by
Andrey
Telerik team
Share this question
or