I have a class which is sort of like a repository, and every time I write to the zip archive using this command, the prior activity is overwritten.

        /// <summary>
        /// Insert file into existing archive file. 
        /// </summary>
        /// <param name="fullPath"></param>
        /// <param name="stream"></param>
        public void InsertIntoFile(String fullPath, Stream stream) {
            using (var fs = new FileStream(FullArchivalPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) {
                using (var zipOutput = new ZipOutputStream(fs)) {
                    zipOutput.SetLevel(9);

                    if (fullPath.Contains(@"\")) {
                        ZipEntry zipDir = new ZipEntry(Path.GetDirectoryName(fullPath) + @"\");
                        zipDir.DateTime = DateTime.Now;
                        zipOutput.PutNextEntry(zipDir);
                    }

                    ZipEntry zipEntry = new ZipEntry(fullPath);
                    zipEntry.DateTime = DateTime.Now;

                    zipOutput.PutNextEntry(zipEntry);

                    var length = (int)stream.Length;
                    byte[] content = new byte[length];
                    stream.Read(content, 0, length);

                    zipOutput.Write(content, 0, length);
                    zipOutput.CloseEntry();
                    zipOutput.Finish();
                    zipOutput.Close();
                }
            }
        }//end method

The following code fixes the problem.

    /// <summary>
    /// Copies file to archival directory. 
    /// </summary>
    /// <param name="fileName"></param>
    public void CopyFile(String fileName) {
        var stream = SrcRepo.GetById(fileName);

        //plus 1 for preceding slash character in the path
        String pathInsideArchive = fileName.Substring(FileSystemPath.Length + 1, fileName.Length - FileSystemPath.Length - 1);

        DestRepo.Insert(pathInsideArchive, stream);
    }//end method

    /// <summary>
    /// Insert file into existing archive file. 
    /// </summary>
    /// <param name="fullPath"></param>
    /// <param name="stream"></param>
    public void InsertIntoFile(String fullPath, Stream stream) {
        using (var fs = new FileStream(FullArchivalPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) {
            using (var zipFile = new ZipFile(fs)) {
                zipFile.BeginUpdate();

                var dataSource = new CustomDataSource();
                dataSource.SetStream(stream);

                zipFile.Add(dataSource, fullPath);

                zipFile.CommitUpdate();
                zipFile.Close();

            }
        }
    }//end method

    /// <summary>
    /// A do nothing class necessary for some functionality in the prior code. 
    /// </summary>
    public class CustomDataSource : IStaticDataSource {
        private Stream stream;

        public CustomDataSource() { }

        public CustomDataSource(Stream stream) {
            SetStream(stream);
        }

        public Stream GetSource() {
            return stream;
        }

        public void SetStream(Stream stream) {
            this.stream = stream;
            this.stream.Position = 0;
        }
    }//end class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.