Example #1
0
        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
        }
Example #2
0
        /// <summary>
        /// Returns 0 success
        /// </summary>
        /// <returns></returns>
        public int Update()
        {
            if (path != null && path.Length > 0)
            {
                if (initialCount > table.Count)
                {
                    return(ReWrite());
                }
                if (!modified)
                {
                    DialogResult res = JMessageBox.Show(PackView.Instance, "The archive has not been modified do you wish to rewirte it?", "Info", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                    if (res == DialogResult.Yes)
                    {
                        return(ReWrite());
                    }
                    else
                    {
                        return(0);
                    }
                }
                this.Dispose();
                PCKStream stream = new PCKStream(path);

                if (stream != null)
                {
                    stream.Seek(fileTableOffset, SeekOrigin.Begin);
                    //Write new files
                    PckEntry[] values = table.Values.ToArray();
                    for (int i = 0; i < values.Length; i++)
                    {
                        PckEntry fte = values[i];
                        if (fte.bytesCompresed != null)
                        {
                            fte.fileoffset = (uint)stream.Position;
                            stream.WriteBytes(fte.bytesCompresed);
                        }
                    }
                    uint fileTableOffset_tmp = (uint)stream.Position;
                    //RewriteTable
                    progress_bar("Finalizing Pack...", 0, 0);
                    MemoryStream FileTable = new MemoryStream();
                    for (int i = 0; i < values.Length; i++)
                    {
                        PckEntry fte    = values[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, values.Length);
                    }
                    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);
                    foreach (PckEntry fte in table.Values)
                    {
                        table[fte.id].bytes          = null;
                        table[fte.id].bytesCompresed = null;
                        table[fte.id].memory         = null;
                    }
                    GC.Collect();
                    return(0);//SUccess
                }
                else
                {
                    return(3);//CANNOT OPEN STREAM;
                }
            }
            else
            {
                if (path == null)
                {
                    return(1); //PATH NULL
                }
                else if (path != null && path.Length == 0)
                {
                    return(1); //PATH NULL
                }
                else
                {
                    return(2);// NOT MODIFIED
                }
            }
        }