1

テキストボックス付きのフォームがあり、顧客はこのテキストボックスからzipアーカイブへのすべての変更を保存したいと考えています。

私はhttp://dotnetzip.codeplex.com を使用しており、コードの例があります。

 using (ZipFile zip = new ZipFile())
  {
    zip.AddFile("text.txt");    
    zip.Save("Backup.zip");
  }

そして、私は毎回temp text.txtを作成し、それを圧縮して戻したくありません。zipファイル内のストリームとしてtext.txtにアクセスし、そこにテキストを保存できますか?

4

2 に答える 2

1

メソッドで Stream を使用する DotNetZip の例があります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 ファイルを作成できることが示されています。

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");  
        }
     }
}
于 2013-02-03T10:46:11.913 に答える
0

引数としてファイル パスを受け入れる別の DotNetZip メソッドがあります。

   zip.RemoveEntry(entry);
   zip.AddEntry(entry.FileName, text, ASCIIEncoding.Unicode);
于 2013-02-03T14:56:03.557 に答える