C# 读取“Zip” 带有 FileStream 的文件

发布于 2024-07-11 10:38:32 字数 221 浏览 11 评论 0原文

我编写了一个程序,该程序将使用 TCPClient 与远程计算机建立网络连接,我使用它以 100k 块的形式将文件传输到远程 .net 应用程序,然后它将它们写入硬盘。 除了 ZIP 文件之外,所有文件传输都运行良好 - 奇怪的是,重新组合的文件始终为 98K...ZIP 文件是否存在某些秘密,阻止以这种方式处理它们。 同样,所有其他文件传输都工作正常,图像、xls、txt、chm、exe 等。

困惑

I have written a program that will etablish a network connection with a remote computer using TCPClient I am using it to transfer files in 100k chunks to a remote .net application and it inturn then writes them to the HardDrive. All file transfers work good except when it comes to ZIP files - it is curious to note that the reasembled file is always 98K...is there some dark secret to ZIP files that prevent them from being handled in this manner. Again all other file transfers work fine, image, xls, txt, chm, exe etc.

Confused

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

年华零落成诗 2024-07-18 10:38:32

好吧,您还没有显示任何代码,因此很难准确说出问题所在。

常见错误是假设 Stream.Read 读取了您要求它读取的所有数据,而不是意识到它可能会读取更少的数据,但它实际读取的数据量是返回值。

换句话说,代码不应该是:

byte[] buffer = new byte[input.Length];
input.Read(buffer, 0, buffer.Length);
output.Write(buffer, 0, buffer.Length);

而是类似:

byte[] buffer = new byte[32 * 1024];
int bytesRead;
while ( (bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
{
    output.Write(buffer, 0, bytesRead);
}

但这只是一个猜测。 如果您可以发布一些代码,我们就有更好的机会解决这个问题。

Well, you haven't shown any code so it's kinda tricky to say exactly what's wrong.

The usual mistake is to assume that Stream.Read reads all the data you ask it to instead of realising that it might read less, but that the amount it actually read is the return value.

In other words, the code shouldn't be:

byte[] buffer = new byte[input.Length];
input.Read(buffer, 0, buffer.Length);
output.Write(buffer, 0, buffer.Length);

but something like:

byte[] buffer = new byte[32 * 1024];
int bytesRead;
while ( (bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
{
    output.Write(buffer, 0, bytesRead);
}

But that's just a guess. If you could post some code, we'd have a better chance of figuring it out.

人间不值得 2024-07-18 10:38:32

实际的代码会很有帮助。

您使用 BinaryReader / BinaryWriter 吗?

(即基于数据而不是基于文本)。

您可以尝试使用十六进制文件比较(例如Beyond Compare)来比较原始文件和副本,看看是否可以给你任何线索。

The actual code would be helpful.

Are you using BinaryReader / BinaryWriter?

(i.e. data based rather than text based).

You could try using a hex file compare (e.g. Beyond Compare) to compare the original and copy and see if that gives you any clues.

Bonjour°[大白 2024-07-18 10:38:32

您可能会用收到的每个块覆盖(而不是附加)现有文件? 因此文件的最终大小将<=一个块的大小。

但如果没有任何代码,很难说出问题的原因。

It might be that you are overwriting (instead of appending to) the existing file with each chunk received? Therefore the file's final size will be <= the size of one chunk.

But without any code, it's difficult to tell the reason for the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文