【发布时间】:2014-10-31 20:12:15
【问题描述】:
zlib 是一个用 Visual c++ 编写的应用程序,可在线获取。
我有一个在 Visual Studio C++ 应用程序中使用 zlib 压缩的文件。现在我正在尝试编写一个完全不同的 c# 程序来在可视 c# 应用程序中解压缩它。我所做的是:
我有一个扩展名为 .fdd 的大文件,其中包含 3 个压缩(使用 zlib)文件(ffo、sym 和 cff)。
首先,我将整个 .fdd 文件转换为字节数组。
我找到了三个文件的确切位置。
使用循环,将文件的各个字节复制到另一个数组中。因此,在第一次迭代中,数组存储 ffo 字节,在第二次迭代中,它存储 sym 字节,依此类推。
将复制的字节数组转换为扩展名为 .zip 的文件,以便我将 3 个压缩文件分开。
所以我将文件命名为 Fileffo.zip、Filesym.zip 和 Filecff.zip。
现在我需要解压文件并将解压后的文件命名为 P.ffo、Q.sym 和 R.cff。
我只知道 .fdd 文件已使用 gzip 实用程序压缩。前四个字节包含 ffo 文件的大小,从第五个字节到 ffo 存在的大小(从前四个字节开始),在 ffo 之后,接下来的四个字节包含 sym 文件的大小,然后是 sym 文件,接下来的四个字节包含 cff 文件的大小,然后是 cff 文件本身。这就是为什么我试图从它们之前的相应四个字节中提取相应文件的大小。然后尝试通过循环将三个文件中的字节信息分别保存在数组中。
如何使用 c# 解压缩它们?我不知道使用“zlib”压缩文件时使用的确切扩展名,所以我输入了 .zip 扩展名。
public static void ExtractFile()
{
int i = 0;
const string zipPath = @"C:\Product Development\Development\FFPCAppTool\PCToolForFF\PCToolForFF\bin\x86\Debug\000A1C10000101.fdd";
if (zipPath.Contains("000A1C10000101.fdd"))
{
byte[] arrayWholeFileInBytes = File.ReadAllBytes(zipPath);
long fileLength = new FileInfo(zipPath).Length;
//string testpath = @"C:\Product Development\Development\ExtractedFdd\" + ManuId + "\\" + DeviceIdentifier + "\\" + Revision + ".zip";
const string testzippath = @"C:\Users\Himagau\Desktop\testExtract\Fileffo.zip";
const string extractPath = @"C:\Users\Himagau\Desktop\testExtract\ ";
while (i < fileLength)
{
using (BinaryReader reader = new BinaryReader(new FileStream(zipPath, FileMode.Open)))
{
reader.BaseStream.Seek(i, SeekOrigin.Begin);
reader.Read(arrayWholeFileInBytes, i, 4); //reading value in bytes
n = BitConverter.ToInt32(arrayWholeFileInBytes, i);
//converting first four bytes into a single integer value i.e. size of zip
reader.Read(arrayWholeFileInBytes, i + 4, n);
byte[] array1 = new byte[n]; //passing the size of zip into array size
Array.Copy(arrayWholeFileInBytes, i + 4, array1, 0, n);
File.WriteAllBytes(testzippath, array1);
ZipFile.ExtractToDirectory(testzippath, extractPath);
i = i + n + 4;
}
}
}
}
zipfile 和 zip 存档出现 central directory not found 之类的错误,当将文件的扩展名从 .zip 更改为 .gz 时,gzipstream 将错误显示为“不是有效的 gzip 文件”。
【问题讨论】:
-
我在
extractPath的值中添加了一个空格,这样网站上的颜色就不会乱了。只是要注意。 -
你看过GZipStream
标签: c# zlib compression