- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我似乎无法通过 StreamWriter 将文本写入新创建的 zip 文件(不是 gzip)。我使用 SharpZipLib 并且不太理解如何让它工作。 DJ Kraze 帮助我将压缩文本文件中的内容流式传输到 StreamReader,我现在尝试相反的方法。我不想先创建一个 csv 文件然后压缩最终文件,而是想将文本直接流式传输到 zip 容器中要创建的 csv。那可能吗?在我用来获取可与 StreamReader 一起使用的流的片段下方,它只是让我了解我在寻找什么,只是这次我想获取一个与 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))
{
最佳答案
第二个例子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;
}
}
如果您可以访问该站点,则有一个调用该代码的示例。
关于c# - SharpZipLib 无法使用 StreamWriter 将文本写入新创建的 csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9435691/
StreamWriter.Flush() 和 StreamWriter.Close() 在功能上有什么区别? 当我的数据没有正确写入文件时,我将 Flush() 和 Close() 添加到我的代码末尾
我需要尽快写入许多文件,但我没有像我使用的那样使用 Streamwriter 获得我想要的结果。例如,如果我写... $File = "c:\somefile" $Contents = get-con
我正在尝试创建一个程序,将文本文件中的几行从文本框编辑为用户设置值。 至少,在我开始使用这个值设置之前。我什至无法编辑该文件。这段代码有什么问题?我实际上尝试了更多示例,但没有一个起作用。 priva
遇到一个问题,streamwriter 在我正在生成的 csv 中生成错误的字符。字符  只出现在文件的开头: 5,"GEN",555555555,,"Evan","Smith",,,,,
我正在尝试创建一个程序,从文本框中将文本文件中的几行编辑为用户设置的值。 至少,在我懒得开始这个值设置之前。我什至无法编辑文件。这段代码有什么问题?我实际上尝试了更多示例,但没有一个有效。 priva
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
如何写入不同类的文件? public class gen { public static string id; public static string m_graph_file; } s
我正在使用 StreamWriter 类将一些文本写入新创建的文本文件,但输出总是出奇地错误,这是我的代码: StreamWriter sw = new StreamWriter("newtextfi
basicLog 是名称和时间戳的列表。我想将它们写入文件。我得到的两个错误是在 ';'在 StreamWriter 行和"file"上。在第二行。 ';'错误是:可能错误的空语句文件错误是:当前上下
这是我的代码。 : FileStream fileStreamRead = new FileStream(pathAndFileName, FileMode.OpenOrCreate, FileAcc
我已经初始化了 StreamWriter 实例并将其与“MyFile.txt”相关联 System.IO.StreamWriter sw = new StreamWriter("MyFile.txt"
我在字符串变量中有大量内容,我想使用流写入器将该内容写入文本文件。但是流编写器正在截断文本而不是将整个内容写入文件。为什么? using (StreamWriter sw = new StreamWr
我有一种方法可以将文件作为附件发送。我使用 StreamWriter 和 MemoryStream 来创建附件。代码如下: public void ComposeEmail(string from,
我试图每小时创建一个新的日志文件,并在服务器上运行以下代码。当天的第一个日志文件正在创建并写入正常,但当天没有创建更多的日志文件。任何想法可能出了什么问题?也不会抛出异常。 private void
我正在为我的游戏制作一个关卡编辑器,大部分工作正常,除了...当我尝试保存我的文件 (XML) 时,文件没有创建,在输出框中我得到: “System.NullReferenceException”类型
我在 form1 中有一个文本框和一个按钮,当我在文本框中书写时,我可以使用我的按钮将其保存到计算机上的文件中。这是放在我的按钮里面 public void button1_Click(obj
我目前的申请有问题,我开始认为这只是我的逻辑。即使浏览了这些表格和 MSDN,我也无法弄清楚。 我正在尝试使用 StreamWriter 在我的应用程序目录中创建文本文档并创建包含该文档的子文件夹。目
我组织了一个小类来实现一个快速记录器。我用一个在构造函数中实例化的私有(private) System.IO.StreamWriter 组成它。因为我使用的方式阻止我实现 using block ,所
我有一个使用此方法的 C# .NET 3.5 Windows 窗体程序: private void toLog(string sLog) { richTextBox1.
这里真的很迷茫。我正在运行 Windows 7 并使用管理员帐户,但由于某种原因,以下代码正在下降; public static readonly string Report = Path.Combi
我是一名优秀的程序员,十分优秀!