我正在设备上运行服务,并希望将文件发送到该服务。很简单,但我想发送的文件来自一个zip文件,这对我来说很复杂。 我试图通过将内部文件的流发送到设备来实现我的目标,但后来我必须意识到我发送了12个字节(顺便说一下,内部文件名的长度是扩展名 - 巧合? )超过GetEntry()。长度表示。
我完全错过了什么或者我做错了什么? 这是当前代码,因为它是(注意:此时客户端是一个有效且连接的tcp-socket):
System.IO.Compression.ZipArchive zfile = System.IO.Compression.ZipFile.Open(_STR_FILENAME, System.IO.Compression.ZipArchiveMode.Read);
System.IO.Compression.ZipArchiveEntry zentry = zfile.GetEntry(_STR_FWNAME);
using (Stream fs = zentry.Open())
{
using(NetworkStream ns = new NetworkStream(client))
{
int i, counter = 0;
while((i = fs.ReadByte()) != -1)
{
ns.WriteByte((byte)i);
counter++;
}
Console.WriteLine("BYTES: " + counter);
Console.WriteLine("LENGTH FILE: " + zentry.Length);
}
}
答案 0 :(得分:0)
您发布的代码中没有任何内容可以解释您最终获得不同计数的原因。所以无论发生什么,它都包含在你没有包含的代码中,或者你发布的代码不是你正在使用的实际代码。
以下是一些只打开.zip文件的代码,并显示可以从存储流中读取的实际字节旁边的存储长度:
static void CheckZipEntries(string fileName)
{
using (Stream inputStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete))
using (ZipArchive archive = new ZipArchive(inputStream, ZipArchiveMode.Read))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
using (Stream entryStream = entry.Open())
{
Console.WriteLine("Entry length: {0}, Stream length: {1}",
entry.Length, GetStreamLength(entryStream));
}
}
}
}
static int GetStreamLength(Stream stream)
{
int count = 0, bytesRead;
byte[] rgb = new byte[1024];
while ((bytesRead = stream.Read(rgb, 0, rgb.Length)) > 0)
{
count += bytesRead;
}
return count;
}
当我在任意数量的.zip文件上运行时,每个归档条目的两个数字都是相同的。
所以,我想你的问题的答案是,不,它实际上并没有这样做。 :)
如果这个答案没有提供足够有用的信息,那么你应该发布一个好的代码示例,一个完整但不超过绝对必要的代码示例。有关为何以及如何执行此操作的信息,请参阅https://stackoverflow.com/help/mcve。