且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否有可能直接在压缩文件中更改文件内容?

更新时间:2023-11-15 09:58:40

目前是使用流的方法的AddEntry 在DotNetZip一个例子。

There is an example in DotNetZip that use a Stream with the method AddEntry.

String zipToCreate = "Content.zip";
String fileNameInArchive = "Content-From-Stream.bin";
using (System.IO.Stream streamToRead = MyStreamOpener())
{
  using (ZipFile zip = new ZipFile())
  {
    ZipEntry entry= zip.AddEntry(fileNameInArchive, streamToRead);
    zip.Save(zipToCreate);  // the stream is read implicitly here
  }
}

一个小测试使用LinqPad表明,它是可以使用的MemoryStream建立zip文件

A little test using LinqPad shows that it is possible to use a MemoryStream to build the zip file

void Main()
{
    UnicodeEncoding uniEncoding = new UnicodeEncoding();
    byte[] firstString = uniEncoding.GetBytes("This is the current contents of your TextBox");
    using(MemoryStream memStream = new MemoryStream(100))
    {
        memStream.Write(firstString, 0 , firstString.Length);
        // Reposition the stream at the beginning (otherwise an empty file will be created in the zip archive
        memStream.Seek(0, SeekOrigin.Begin);
        using (ZipFile zip = new ZipFile())
        {
            ZipEntry entry= zip.AddEntry("TextBoxData.txt", memStream);
            zip.Save(@"D:\temp\memzip.zip");  
        }
     }
}