Skip to content

Reading an archive content and then adding no files to it causes archive corruption #22

Closed
@xtomino

Description

@xtomino

Thank you very much for developing ZipStorer. I have used it in several apps and until now I haven't encountered a problem.

In my last project I needed check a content of archive before add new files to avoid duplicate file names in that archive. I find out if I read an archive content and then I add no files into it to avoid duplicates causes an archive corruption.

See my test code. After first run a new archive is created. After second run an existing archive is opened and after check for duplicate file names in archive no files are added. And after that the whole archive is corrupted! And I opened only an existing archive, then I read its content, I wrote down no files and in the end I close it.

Do you have an idea where the problem might be?

const string archiveFilePath = @"C:\Temp\test.zip";
var archiveFileExists = File.Exists(archiveFilePath);
using (var zipArchive = archiveFileExists
  ? ZipStorer.Open(archiveFilePath, FileAccess.ReadWrite)
  : ZipStorer.Create(archiveFilePath, "TestArchive"))
{
  var zipFileEntries = archiveFileExists
    ? zipArchive.ReadCentralDir()
    : new List<ZipStorer.ZipFileEntry>();
  for (int i = 0; i < 10; i++)
  {
    if (zipFileEntries.Any(e => e.FilenameInZip == $"TestFile_{i + 1:00}.txt")) continue;
    using (var memoryStream = new MemoryStream())
    using (var streamWriter = new StreamWriter(memoryStream))
    {
      streamWriter.WriteLine("TEST!");
      streamWriter.Flush();
      memoryStream.Seek(0, SeekOrigin.Begin);
      zipArchive.AddStream(ZipStorer.Compression.Deflate, $"TestFile_{i + 1:00}.txt", memoryStream, 
      DateTime.Now, string.Empty);
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions