public void addFilesToTable(string Directory, string FilePaths) { PckEntry data = table.Values.FirstOrDefault(x => x.filePath.ToLower().Equals(Directory)); if (data != null) { data.filePath = Directory; data.bytes = File.ReadAllBytes(FilePaths); data.bytesCompresed = PCKZlib.Compress(data.bytes, CompressionLevel); data.fileoffset = (uint)0; data.compressedSize = data.bytesCompresed.Length; data.decompressedSize = data.bytes.Length; table[data.id] = data; modified = true; } else { data = new PckEntry(version); data.filePath = Directory; data.bytes = File.ReadAllBytes(FilePaths); data.bytesCompresed = PCKZlib.Compress(data.bytes, CompressionLevel); data.fileoffset = (uint)0; data.compressedSize = data.bytesCompresed.Length; data.decompressedSize = data.bytes.Length; data.id = table.Count; table.Add(data.id, data); directoryStructure.addfille(data); modified = true; } }
public byte[] Write(int CompressionLevel) { byte[] buffer = new byte[276]; MemoryStream msb = new MemoryStream(buffer); BinaryWriter bw = new BinaryWriter(msb); bw.Write(Encoding.GetEncoding("GB2312").GetBytes(Path.Replace("/", "\\"))); bw.BaseStream.Seek(260, SeekOrigin.Begin); bw.Write(Offset); bw.Write(Size); bw.Write(CompressedSize); bw.Write(0); bw.BaseStream.Seek(0, SeekOrigin.Begin); bw.Close(); byte[] compressed = PCKZlib.Compress(buffer, CompressionLevel); return(compressed.Length < 276 ? compressed : buffer); }
public int ReWrite() { string pathx = path + ".files\\"; DialogResult result = DialogResult.No; if (Directory.Exists(pathx)) { result = MessageBox.Show("Found files on disk do you want to rewrite from files?", "Question", MessageBoxButtons.YesNo); } #region AAAAA if (result == DialogResult.No) { LimitedConcurrencyLevelTaskScheduler lcts = new LimitedConcurrencyLevelTaskScheduler(Environment.ProcessorCount / 2); List <Task> tasks = new List <Task>(); TaskFactory factory = new TaskFactory(lcts); CancellationTokenSource cts = new CancellationTokenSource(); MakeStreams(); if (path != null && path.Length > 0) { PckEntry[] values = table.Values.ToArray(); int count = values.Length; for (int i = 0; i < values.Length; i++) { if (values[i].bytesCompresed == null) { PckEntry pck = values[i]; byte[] data = getChunk2("", pck); progress_bar("Reading files ....", i, count); Task t1 = factory.StartNew((o) => { progress_bar("Compressing files ....", (int)o, count); pck.bytesCompresed = PCKZlib.Compress(data, CompressionLevel); }, i, cts.Token); tasks.Add(t1); } } progress_bar("Please Wait", 0, 0); Task.WaitAll(tasks.ToArray()); Thread.Sleep(500); cts.Dispose(); initialCount = count; this.Dispose(); if (File.Exists(path)) { File.Delete(path); } if (File.Exists(path.Replace(".pck", ".pkx"))) { File.Delete(path.Replace(".pck", ".pkx")); } PCKStream stream = new PCKStream(path); stream.WriteInt32(stream.key.FSIG_1); stream.WriteInt32(0); stream.WriteInt32(stream.key.FSIG_2); for (int i = 0; i < values.Length; i++) { PckEntry fte = values[i]; fte.setVersion(version); progress_bar("Writing files ....", i, count); fte.fileoffset = (uint)stream.Position; stream.WriteBytes(fte.bytesCompresed); } uint fileTableOffset_tmp = (uint)stream.Position; MemoryStream FileTable = new MemoryStream(); for (int i = 0; i < values.Length; i++) { PckEntry fte = values[i]; fte.setVersion(version); byte[] buffer = fte.Write(CompressionLevel); lock (FileTable) { FileTable.Write(BitConverter.GetBytes(buffer.Length ^ KEY_1), 0, 4); FileTable.Write(BitConverter.GetBytes(buffer.Length ^ KEY_2), 0, 4); FileTable.Write(buffer, 0, buffer.Length); } progress_bar("Writing table...", i, count); } stream.WriteBytes(FileTable.ToArray()); FileTable.Flush(); FileTable.Dispose(); stream.WriteInt32(ASIG_1); stream.WriteInt16(version); stream.WriteInt16(2); stream.WriteUInt32((uint)(fileTableOffset_tmp ^ KEY_1)); if (version > 2) { stream.WriteInt32(-1); } stream.WriteInt32(0); NameChar namechar = new NameChar(252); namechar.DefaultEncoding = Encoding.Default; namechar.Name = "Angelica File Package, writed by JHS Software."; stream.WriteBytes(namechar.EncodedName); stream.WriteInt32(ASIG_2); if (version > 2) { stream.WriteInt32(512); } stream.WriteInt32(table.Count); stream.WriteInt16(version); stream.WriteInt16(2); stream.Seek(4, SeekOrigin.Begin); stream.WriteUInt32((uint)stream.GetLenght()); stream.Dispose(); progress_bar("Clearing memory...", 0, 0); for (int i = 0; i < values.Length; i++) { PckEntry fte = values[i]; table[fte.id].bytes = null; table[fte.id].bytesCompresed = null; table[fte.id].memory = null; progress_bar("Clearing memory...", i, count); } GC.Collect(); progress_bar("Ready", 0, 0); } } else #endregion #region FROM DISK { directoryStructure = null; GC.Collect(); modified = false; directoryStructure = new PackStructureManager(Path.GetFileNameWithoutExtension(path)); table = new SortedList <int, PckEntry>(); string[] files = Directory.GetFiles(pathx, "*", SearchOption.AllDirectories); int count = files.Length; initialCount = count; this.Dispose(); if (File.Exists(path)) { File.Delete(path); } if (File.Exists(path.Replace(".pck", ".pkx"))) { File.Delete(path.Replace(".pck", ".pkx")); } PCKStream stream = new PCKStream(path); stream.WriteInt32(stream.key.FSIG_1); stream.WriteInt32(0); stream.WriteInt32(stream.key.FSIG_2); for (int i = 0; i < files.Length; i++) { string file = files[i].Replace(pathx, "").Replace("/", "\\"); string pathr = Path.Combine(pathx, file); PckEntry data = new PckEntry(version); data.filePath = file; byte[] datab = File.ReadAllBytes(pathr); byte[] bytesCompresed = PCKZlib.Compress(datab, CompressionLevel); data.compressedSize = bytesCompresed.Length; data.decompressedSize = datab.Length; data.id = i; progress_bar("Writing files ....", i, count); data.fileoffset = (uint)stream.Position; stream.WriteBytes(bytesCompresed); table[i] = data; directoryStructure.addfille(table[i]); } progress_bar("Please Wait", 0, 0); Application.DoEvents(); uint fileTableOffset_tmp = (uint)stream.Position; MemoryStream FileTable = new MemoryStream(); progress_bar("Finalizing Pack...", 0, 0); for (int i = 0; i < table.Count; i++) { PckEntry fte = table[i]; byte[] buffer = fte.Write(CompressionLevel); lock (FileTable) { FileTable.Write(BitConverter.GetBytes(buffer.Length ^ KEY_1), 0, 4); FileTable.Write(BitConverter.GetBytes(buffer.Length ^ KEY_2), 0, 4); FileTable.Write(buffer, 0, buffer.Length); } progress_bar("Finalizing Pack...", i, count); } stream.WriteBytes(FileTable.ToArray()); FileTable.Flush(); FileTable.Dispose(); stream.WriteInt32(ASIG_1); stream.WriteInt16(version); stream.WriteInt16(2); stream.WriteUInt32((uint)(fileTableOffset_tmp ^ KEY_1)); if (version > 2) { stream.WriteInt32(-1); } stream.WriteInt32(0); NameChar namechar = new NameChar(252); namechar.DefaultEncoding = Encoding.Default; namechar.Name = "Angelica File Package, writed by JHS Software."; stream.WriteBytes(namechar.EncodedName); stream.WriteInt32(ASIG_2); if (version > 2) { stream.WriteInt32(512); } stream.WriteInt32(table.Count); stream.WriteInt16(version); stream.WriteInt16(2); stream.Seek(4, SeekOrigin.Begin); stream.WriteUInt32((uint)stream.GetLenght()); stream.Dispose(); progress_bar("Clearing memory...", 0, 0); for (int i = 0; i < table.Count; i++) { table[i].bytes = null; table[i].bytesCompresed = null; table[i].memory = null; progress_bar("Clearing memory...", i, count); } GC.Collect(); progress_bar("Ready", 0, 0); } #endregion return(0);//SUccess }