ICSharpCode.SharpZipLib.Zip.ZipFile.SetComment C# (CSharp) Method

SetComment() public method

Set the file comment to be recorded when the current update is commited.
ZipFile has been closed.
public SetComment ( string comment ) : void
comment string The comment to record.
return void
        public void SetComment(string comment) {
            if (isDisposed_) {
                throw new ObjectDisposedException("ZipFile");
            }

            CheckUpdating();

            newComment_=new ZipString(comment);

            if (newComment_.RawLength>0xffff) {
                newComment_=null;
                throw new ZipException("Comment length exceeds maximum - 65535");
            }

            // We dont take account of the original and current comment appearing to be the same
            // as encoding may be different.
            commentEdited_=true;
        }

Usage Example

Example #1
0
        public void UpdateCommentOnlyOnDisk()
        {
            string tempFile = GetTempFilePath();
            Assert.IsNotNull(tempFile, "No permission to execute this test?");

            tempFile = Path.Combine(tempFile, "SharpZipTest.Zip");
            if (File.Exists(tempFile))
            {
                File.Delete(tempFile);
            }

            using (ZipFile testFile = ZipFile.Create(tempFile))
            {
                testFile.BeginUpdate();
                testFile.Add(new StringMemoryDataSource("Aha"), "No1", CompressionMethod.Stored);
                testFile.Add(new StringMemoryDataSource("And so it goes"), "No2", CompressionMethod.Stored);
                testFile.Add(new StringMemoryDataSource("No3"), "No3", CompressionMethod.Stored);
                testFile.CommitUpdate();

                Assert.IsTrue(testFile.TestArchive(true));
            }

            using (ZipFile testFile = new ZipFile(tempFile))
            {
                Assert.IsTrue(testFile.TestArchive(true));
                Assert.AreEqual("", testFile.ZipFileComment);

                testFile.BeginUpdate(new DiskArchiveStorage(testFile, FileUpdateMode.Direct));
                testFile.SetComment("Here is my comment");
                testFile.CommitUpdate();

                Assert.IsTrue(testFile.TestArchive(true));
            }

            using (ZipFile testFile = new ZipFile(tempFile))
            {
                Assert.IsTrue(testFile.TestArchive(true));
                Assert.AreEqual("Here is my comment", testFile.ZipFileComment);
            }
            File.Delete(tempFile);

            // Variant using indirect updating.
            using (ZipFile testFile = ZipFile.Create(tempFile))
            {
                testFile.BeginUpdate();
                testFile.Add(new StringMemoryDataSource("Aha"), "No1", CompressionMethod.Stored);
                testFile.Add(new StringMemoryDataSource("And so it goes"), "No2", CompressionMethod.Stored);
                testFile.Add(new StringMemoryDataSource("No3"), "No3", CompressionMethod.Stored);
                testFile.CommitUpdate();

                Assert.IsTrue(testFile.TestArchive(true));
            }

            using (ZipFile testFile = new ZipFile(tempFile))
            {
                Assert.IsTrue(testFile.TestArchive(true));
                Assert.AreEqual("", testFile.ZipFileComment);

                testFile.BeginUpdate();
                testFile.SetComment("Here is my comment");
                testFile.CommitUpdate();

                Assert.IsTrue(testFile.TestArchive(true));
            }

            using (ZipFile testFile = new ZipFile(tempFile))
            {
                Assert.IsTrue(testFile.TestArchive(true));
                Assert.AreEqual("Here is my comment", testFile.ZipFileComment);
            }
            File.Delete(tempFile);
        }
All Usage Examples Of ICSharpCode.SharpZipLib.Zip.ZipFile::SetComment