/// <summary> /// 解压所有条目,顺序解压 /// </summary> /// <param name="SaveDir">存储目录</param> public void UnZipAll(string SaveDir) { try { //条目表 byte[] EntryTablesBuffer = new byte[EntrysTableLen]; Array.Copy(this.buff, HeadLen, EntryTablesBuffer, 0, EntrysTableLen); //文件数据 byte[] FilesBuffer = new byte[Length - EntrysTableLen - HeadLen]; Array.Copy(this.buff, HeadLen + EntrysTableLen, FilesBuffer, 0, Length - EntrysTableLen - HeadLen); //开始遍历Entry int iTablesOffBits = 0; int iFilesOffBits = 0; uint iCount = 1; while (iCount <= EntryCount) { uint EntryLen = BitConverter.ToUInt32(EntryTablesBuffer, iTablesOffBits); uint FileLen = BitConverter.ToUInt32(EntryTablesBuffer, iTablesOffBits + sizeof(uint) * 2 + sizeof(UInt16) * 2); byte[] temp = new byte[EntryLen - FileLen]; Array.Copy(EntryTablesBuffer, iTablesOffBits, temp, 0, EntryLen - FileLen); Entry _entry = new Entry(temp); byte[] EntryFileBuffer = new byte[_entry.FileLen]; Array.Copy(FilesBuffer, iFilesOffBits, EntryFileBuffer, 0, _entry.FileLen); string FileName = Path.Combine(SaveDir, BitConvertHelper.Byte2String(_entry.EntryDir, 0, _entry.EntryDirLen)); if (!Directory.Exists(FileName)) { Directory.CreateDirectory(FileName); } FileName = Path.Combine(FileName, BitConvertHelper.Byte2String(_entry.EntryName, 0, _entry.EntryNameLen)); FileStream fsSave = new FileStream(FileName, FileMode.Create); Lz77Compression lz77 = new Lz77Compression(); byte[] btyFile = lz77.DeCompress(EntryFileBuffer); fsSave.Write(btyFile, 0, btyFile.Length); //fsSave.Write(EntryFileBuffer, 0, EntryFileBuffer.Length); fsSave.Close(); iTablesOffBits += (int)(EntryLen - FileLen); iFilesOffBits += (int)_entry.FileLen; iCount++; } } catch (Exception ex) { throw new Exception("解压缩文件错误!", ex); } }
/// <summary> /// 通过指定文件构造一个条目 /// </summary> /// <param name="FileName">需要压缩的文件</param> /// <param name="ZipInnerDir">压缩包里的路径</param> public Entry(string FileName, string ZipInnerDir) { System.IO.FileStream fs = null; try { fs = new FileStream(FileName, FileMode.Open, FileAccess.Read); } catch (FileNotFoundException exFnf) { if (fs != null) { fs.Close(); } throw exFnf; } byte[] file = new byte[(int)fs.Length]; fs.Read(file, 0, (int)fs.Length); fs.Close(); this.filebuff = new Lz77Compression().Compress(file); FileLen = (uint)filebuff.Length; Length += FileLen; string sFileName = Path.GetFileName(FileName); EntryName = BitConvertHelper.String2Byte(sFileName); EntryNameLen = (ushort)EntryName.Length; Length += EntryNameLen; EntryDir = BitConvertHelper.String2Byte(ZipInnerDir); EntryDirLen = (ushort)EntryDir.Length; Length += EntryDirLen; //Buff处理,不包括文件 byte[] temp = new byte[Length - FileLen]; BitConverter.GetBytes(Length).CopyTo(temp, 0); BitConverter.GetBytes(EntryNameLen).CopyTo(temp, 4); BitConverter.GetBytes(EntryDirLen).CopyTo(temp, 6); BitConverter.GetBytes(offset).CopyTo(temp, 8); BitConverter.GetBytes(FileLen).CopyTo(temp, 12); EntryName.CopyTo(temp, 16); EntryDir.CopyTo(temp, 16 + EntryName.Length); buff = temp; }