Пример #1
0
        public static string Load(string packPath)
        {
            if (IOManager.FileExists(packPath))
            {
                try
                {
                    Stream           archStream = IOManager.GetStream(packPath);
                    ZipArchive       arch       = new ZipArchive(archStream);
                    XmlSerializer    xs         = new XmlSerializer(typeof(ResourcePackInfo));
                    Stream           s          = arch.GetEntry("Info.xml").Open();
                    ResourcePackInfo info       = (ResourcePackInfo)xs.Deserialize(s);
                    info.ResourceData      = packPath;
                    LoadedPacks[info.Name] = info;
                    s.Dispose();
                    arch.Dispose();
                    archStream.Dispose();
                    return(info.Name);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }

            return("");
        }
Пример #2
0
        public static void Create(
            string folder, string targetFile, string name, string unpackerConfig, IProgressIndicator ProgressInfo)
        {
            ResourcePackInfo info = new ResourcePackInfo
            {
                Name           = name,
                UnpackerConfig = unpackerConfig
            };

            ProgressInfo?.SetProgress("Writing Package Info: " + info, 1, 2);
            XmlSerializer xs = new XmlSerializer(typeof(ResourcePackInfo));
            Stream        s  = File.OpenWrite(Path.Combine(folder, "Info.xml"));

            xs.Serialize(s, info);
            s.Dispose();

            ProgressInfo?.SetProgress("Creating Package from Folder: " + folder, 1, 2);

            ZipArchive arch = new ZipArchive(File.OpenWrite(targetFile), ZipArchiveMode.Create);

            string[] files = Directory.GetFiles(folder, "*", SearchOption.AllDirectories);

            IProgressIndicator perFileProgressIndicator = ProgressInfo?.CreateSubTask();

            for (int i = 0; i < files.Length; i++)
            {
                perFileProgressIndicator?.SetProgress("Packing File: " + files[i], i, files.Length - 1);
                try
                {
                    string zipLocation = files[i].Replace(
                        folder.EndsWith("\\") || folder.EndsWith("/")
                                                              ? folder
                                                              : folder + "\\",
                        ""
                        );
                    ZipArchiveEntry entry   = arch.CreateEntry(zipLocation);
                    Stream          filestr = File.OpenRead(files[i]);
                    Stream          zipstr  = entry.Open();
                    filestr.CopyTo(zipstr);
                    filestr.Close();
                    zipstr.Close();
                }
                catch (Exception)
                {
                }
            }

            arch.Dispose();

            File.Delete(Path.Combine(folder, "Info.xml"));
        }