【问题标题】:SharpZipLib cannot use StreamWriter to write text to newly created csv fileSharpZipLib 无法使用 StreamWriter 将文本写入新创建的 csv 文件
【发布时间】:2012-03-15 04:40:32
【问题描述】:

我似乎无法通过 StreamWriter 将文本写入新创建的 zip 文件(不是 gzip)。我使用 SharpZipLib,但不太明白如何让它工作。 DJ Kraze 帮助我将压缩文本文件中的内容流式传输到 StreamReader,我现在尝试相反。我不想先创建一个 csv 文件,然后压缩最终文件,而是想将文本直接流式传输到 zip 容器中要创建的 csv。那可能吗?在我用于获取可与 StreamReader 一起使用的流的 sn-p 下方,它只是给出了我正在寻找的东西的想法,只是这次我想获取与 StreamWriter 一起使用的流。

public static Stream GetZipInputFileStream(string fileName)
{
    ZipInputStream zip = new ZipInputStream(File.OpenRead(fileName));
    FileStream filestream = 
        new FileStream(fileName, FileMode.Open, FileAccess.Read);
    ZipFile zipfile = new ZipFile(filestream);
    ZipEntry item;

    if ((item = zip.GetNextEntry()) != null)
    {
        return zipfile.GetInputStream(item);
    }
    else
    {
        return null;
    }
}

这是我使用它的方式,我基本上是在寻找它,但反过来(StreamWriter -> 新 zip 容器中的新 csv 文件):

using (StreamReader streamReader = Path.GetExtension(fileName).ToUpper().Equals(".ZIP") ? new StreamReader(FileOperations.GetZipInputFileStream(fileName)) : new StreamReader(fileName))
            {

【问题讨论】:

  • 请不要在标题前加上“c#”之类的前缀。这就是标签的用途。
  • 这不是问题。我尝试通知新的Stack Overflow 用户,这样他们就不会在未来的游戏中这样做。为我节省了大量编辑标题的工作。

标签: c# streamwriter sharpziplib


【解决方案1】:

第二个示例here 解决了将流直接写入 SharpZipLib 中的 zip 文件的问题。快速浏览一下,让我们知道它是如何为您工作的。

编辑:由于链接有问题,下面是来自 wiki 的示例。

public void UpdateZipInMemory(Stream zipStream, Stream entryStream, String entryName) 
{

    // The zipStream is expected to contain the complete zipfile to be updated
    ZipFile zipFile = new ZipFile(zipStream);

    zipFile.BeginUpdate();

    // To use the entryStream as a file to be added to the zip,
    // we need to put it into an implementation of IStaticDataSource.
    CustomStaticDataSource sds = new CustomStaticDataSource();
    sds.SetStream(entryStream);

    // If an entry of the same name already exists, it will be overwritten; otherwise added.
    zipFile.Add(sds, entryName);

    // Both CommitUpdate and Close must be called.
    zipFile.CommitUpdate();

    // Set this so that Close does not close the memorystream
    zipFile.IsStreamOwner = false;
    zipFile.Close();

    // Reposition to the start for the convenience of the caller.
    zipStream.Position = 0;
}

以及配套的数据结构

public class CustomStaticDataSource : IStaticDataSource
{
    private Stream _stream;

    // Implement method from IStaticDataSource
    public Stream GetSource() { return _stream; }

    // Call this to provide the memorystream
    public void SetStream(Stream inputStream) 
    {
        _stream = inputStream;
        _stream.Position = 0;
    }
}

如果您可以访问该站点,则有一个调用该代码的示例。

【讨论】:

  • 奇数。对于那个很抱歉。尝试粘贴:wiki.sharpdevelop.net/SharpZipLib_Updating.ashx
  • 感谢您的代码,但我想我还是迷路了。我看不到它如何让我访问可以通过 StreamWriter 写入的流。我需要 StreamWriter sw = new StreamWriter(->
  • 我编辑并添加了我如何将我的 sn-p 与 StreamReader 结合使用。我正在寻找与 StreamWriter 一起使用的同样优雅的解决方案。
  • TimW,此代码无法解决我的问题。虽然它演示了如何将完成的(!)流写入现有的完成(!) zip 流,但它没有解决如何提供 StreamWriter 可以用来直接写入 zip 容器的流。
  • 将部分文件写入 zip 流是否重要?或者您只是想避免将文件写入磁盘?如果是后者,您可以将 StreamWriter 指向 MemoryStream,使用 StreamWriter 写入文件,然后在完成后将底层流保存到 zip 流中。
【解决方案2】:

为此,我最终转储了 SharpZipLib,而是采用了更占用空间的方法,首先解压缩 zip 容器中的所有文件,处理数据,然后将文件移回 zip 容器。如上所述,我面临的问题是我无法一次读取容器中的任何文件,因为它们很大。很高兴看到一个 zip 库将来可能能够处理部分流写入到容器中,但现在我没有看到用 SharpZipLib 完成它的方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    • 2011-01-30
    • 2014-02-22
    • 2018-08-24
    • 2020-01-08
    • 2020-05-23
    相关资源
    最近更新 更多