zlib是一個用visual C++編寫的應用程序,可在線使用。在c#中使用zlib壓縮文件時解壓文件
我有一個在visual studio C++應用程序中使用zlib壓縮的文件。現在我正在嘗試編寫一個完全不同的c#程序來在visual c#應用程序中解壓縮它。我所做的是:
我有一個包含3壓縮(使用zlib的)文件(FFO,符號和CFF).fdd擴展大文件。
首先,我將整個.fdd文件轉換爲一個字節數組。
我找到了三個文件的確切位置。
使用循環,將文件的各個字節複製到另一個數組中。因此,在第一次迭代中,數組存儲ffo字節,在第二次迭代中存儲sym字節等。
將複製的字節數組轉換爲帶.zip擴展名的文件,以便分離出3個壓縮文件。
因此,我將文件命名爲Fileffo.zip,Filesym.zip和Filecff.zip。
現在我需要解壓文件並將解壓文件命名爲P.ffo,Q.sym和R.cff。
我只知道使用gzip實用工具壓縮了.fdd文件。前四個字節包含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;
}
}
}
}
zip文件& zip壓縮包是給了這樣的錯誤central directory not found
當改變文件的擴展名從.zip文件。廣州則gzipstream是給錯誤的「不是一個有效的gzip文件。」
我在'extractPath'的值中增加了一個空格,這樣本網站的着色就不會被嚇倒了。只是要知道。 – gunr2171 2014-10-31 20:17:56
你看過[GZipStream](http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream(v = vs.110).aspx) – 2014-10-31 23:24:51