public static void BuildPackage(List<string> path, string savePath, System.Windows.Forms.ProgressBar pb) { string fileName = savePath + "\\"; SgPack.Clear(); //========================================== // Delete old files //========================================== string savepath = savePath + "\\"; for (int i = 100; i < 200; i++) { if (File.Exists(savepath + "res." + i.ToString())) { File.Delete(savepath + "res." + i.ToString()); } } for (int i = 10; i < 100; i++) { if (File.Exists(savepath + "res.0" + i.ToString())) { File.Delete(savepath + "res.0" + i.ToString()); } } for (int i = 0; i < 10; i++) { if (File.Exists(savepath + "res.00" + i.ToString())) { File.Delete(savepath + "res.00" + i.ToString()); } } //========================================== // Pack new files //========================================== pb.Value = 0; pb.Maximum = path.Count(); foreach (string pt in path) { SgdataIndex inx = new SgdataIndex(); FileInfo fi = new FileInfo(pt); inx.DataId = GetFileNumber(Fhash.EncodeFileName(Path.GetFileName(pt))); inx.Hash = Fhash.EncodeFileName(Path.GetFileName(pt)); inx.Name = Path.GetFileName(pt); inx.Size = (int)fi.Length; //Datei schreiben FileStream fstr = new FileStream(fileName + "res." + inx.DataId, FileMode.Append); BinaryWriter bwrite = new BinaryWriter(fstr); inx.Offset = (int)fstr.Position; byte[] bytes = File.ReadAllBytes(pt); string extension = Path.GetExtension(pt); if (extension != null && Encrypted(extension.Replace(".",""))) { // encrypt byte xorss = 0; for (int i = 0; i < bytes.Length; i++) { bytes[i] ^= Xor[xorss]; xorss++; } bwrite.Write(bytes); } else { //save without encryption bwrite.Write(bytes); // bwrite.Write } SgPack.Add(inx); bwrite.Close(); fstr.Close(); pb.Value += 1; System.Windows.Forms.Application.DoEvents(); } //========================================== // build res.000 (index) //========================================== FileStream fs = new FileStream(fileName + "res.000", FileMode.CreateNew); BinaryWriter w = new BinaryWriter(fs); byte xors = 0; foreach (SgdataIndex pt in SgPack) { byte[] hashedfilename = Sfm.StringToBytes(pt.Hash); int hashlenght = hashedfilename.Length; int fileSize = (int)pt.Size; int offset = pt.Offset; byte[] data = new byte[8]; byte[] off = BitConverter.GetBytes(offset); byte[] fz = BitConverter.GetBytes(fileSize); data[0] = off[0]; data[1] = off[1]; data[2] = off[2]; data[3] = off[3]; data[4] = fz[0]; data[5] = fz[1]; data[6] = fz[2]; data[7] = fz[3]; hashlenght ^= Xor[xors]; xors++; for (int i = 0; i < hashedfilename.Length; i++) { hashedfilename[i] ^= Xor[xors]; xors++; } for (int i = 0; i < 8; i++) { data[i] ^= Xor[xors]; xors++; } w.Write((byte)hashlenght); w.Write(hashedfilename); w.Write(data); //Hashlänge(1 byte) -- hash(Hashlänge) -- data(8 Byte) {<------Offset----->} // {..........Size---->} } w.Close(); fs.Close(); }
public static void Open(string res000File) { SgData.Clear(); try { FileStream fs = new FileStream(res000File, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); uint fsl = (uint)fs.Length; uint cp = 0; byte xors = 0; while (cp < fsl) { SgdataIndex sgIndex = new SgdataIndex(); byte size = br.ReadByte(); cp++; size ^= Xor[xors]; xors++; byte[] hash = br.ReadBytes(size); for (int i = 0; i < size; i++) { hash[i] ^= Xor[xors]; xors++; cp++; } byte[] data = br.ReadBytes(8); for (int i = 0; i < 8; i++) { data[i] ^= Xor[xors]; xors++; cp++; } sgIndex.Name = Fhash.DecodeFileName(Sfm.BytesToString(hash)); sgIndex.Hash = Sfm.BytesToString(hash); sgIndex.Size = BitConverter.ToInt32(data, 4); sgIndex.DataId = GetFileNumber(sgIndex.Hash); sgIndex.Offset = BitConverter.ToInt32(data, 0); SgData.Add(sgIndex); } fs.Close(); fs.Dispose(); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message, "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); } }