Exemplo n.º 1
0
        } // End UnZip

        /// <summary>
        /// Unzip a stream that represents a zip file and return the first entry as a stream
        /// </summary>
        public static Stream UnzipStream(Stream zipstream, out ZipFile zipFile, string entryName = null)
        {
            zipFile = ZipFile.Read(zipstream);

            try
            {
                Ionic.Zip.ZipEntry entry;
                if (string.IsNullOrEmpty(entryName))
                {
                    //Read the file entry into buffer:
                    entry = zipFile.Entries.FirstOrDefault();
                }
                else
                {
                    // Attempt to find our specific entry
                    if (!zipFile.ContainsEntry(entryName))
                    {
                        return(null);
                    }
                    entry = zipFile[entryName];
                }

                if (entry != null)
                {
                    return(entry.OpenReader());
                }
            }
            catch (Exception err)
            {
                Log.Error(err);
            }

            return(null);
        } // End UnZip
Exemplo n.º 2
0
        private void MergeModpackJar()
        {
            DeleteTempFolder();

            Directory.CreateDirectory(cd + "\\temp");
            File.Delete(cd + "\\plugins\\mergedjar\\modpack.jar");

            foreach (string file in Directory.GetFiles(cd + "\\plugins\\forgemodloader", "*forge*.jar"))
            {
                File.Copy(file, cd + "\\plugins\\mergedjar\\modpack.jar");
                break;
            }

            ZipFile.ExtractToDirectory(cd + "\\modpack\\bin\\modpack.jar", cd + "\\temp\\extract");

            using (Ionic.Zip.ZipFile jar = Ionic.Zip.ZipFile.Read(cd + "\\plugins\\mergedjar\\modpack.jar")) {
                if (Directory.GetFiles(cd + "\\temp\\extract", "*.*", SearchOption.AllDirectories).Length > 0)
                {
                    foreach (string file in Directory.GetFiles(cd + "\\temp\\extract", "*.*", SearchOption.AllDirectories))
                    {
                        if (jar.ContainsEntry(file))
                        {
                            jar.RemoveEntry(file);
                        }
                        jar.AddFile(file, file.Replace(Path.GetFileName(file), "").Replace(cd + "\\temp\\extract", ""));
                    }

                    bool succeed = false;

                    while (!succeed)
                    {
                        try {
                            jar.Save();
                            succeed = true;
                        }
                        catch { }
                    }
                }
            }

            DeleteTempFolder();
        }
Exemplo n.º 3
0
 public void createDirectory(String fullPath)
 {
     zipFile.Dispose();
     try
     {
         if (!fullPath.EndsWith("/") && !fullPath.EndsWith("\\"))
         {
             fullPath += '/';
         }
         using (Ionic.Zip.ZipFile ionicZip = new Ionic.Zip.ZipFile(resourceLocation))
         {
             if (!ionicZip.ContainsEntry(fullPath))
             {
                 ionicZip.AddDirectoryByName(fullPath);
             }
             ionicZip.Save();
         }
     }
     finally
     {
         zipFile = new ZipFile(resourceLocation);
     }
 }
Exemplo n.º 4
0
        public static void Main(string[] args)
        {
            LogAttrbute.log.Info("Starting up packager program...");
            Console.WriteLine("Packager v" + typeof(Program).Assembly.GetName().Version.ToString());
            Console.WriteLine("Packager  Copyright (C) 2015 by Mohamed Alkindi\n\tThis program comes with ABSOLUTELY NO WARRANTY; for details type `-l'.\n\tThis is free software, and you are welcome to redistribute it\n\tunder certain conditions.");
            Console.WriteLine();
            if (Information.IsNothing(Settings.Default.ExcludeFileList))
            {
                LogAttrbute.log.Info("Exclude file list value is null, create it");
                Settings.Default.ExcludeFileList = new System.Collections.Specialized.StringCollection();
            }
            if (Information.IsNothing(Settings.Default.ReleaseLocation))
            {
                LogAttrbute.log.Info("Release directory list is null, create it");
                Settings.Default.ReleaseLocation = new System.Collections.Specialized.StringCollection();
            }
            LogAttrbute.log.Info("Save settings");
            Settings.Default.Save();
            if (args.Length > 0)
            {
                ArgumnentsAhead(args);
                Settings.Default.Save();

                return;
            }
            if (Settings.Default.ReleaseLocation == null)
            {
                Console.Error.WriteLine("Release location is not defined. Please locate the release location at least one by -h for instructions");

                return;
            }
            if (Settings.Default.ExcludeFileList.Count <= 0)
            {
                Console.WriteLine("Warning: Exclude list is empty, type -h for argument list");
            }
            if (Settings.Default.ReleaseLocation.Count <= 0)
            {
                Console.WriteLine("Welcome to the release maker program, if this is your first time to open this program, this program help you to packaging your file into zip. when you execute this program, the program is immediately packeging your binaries or your produced files into zip format easily, but first , you must add your release directory list from where you went , you can use gui by type -e --g to open it, or you can use the command line to configuring your custom configuration by type -h for more instructions");
                Console.Error.WriteLine("The release directory list is empty, please enter -h for instructions");

                return;
            }
            foreach (var itemdir in Settings.Default.ReleaseLocation)
            {
                Console.WriteLine("Processing release directory \"" + itemdir);
                DirectoryInfo info = new DirectoryInfo((string)itemdir);
                IList<string> files = new List<string>();
                foreach (var item in info.GetFiles())
                {
                    files.Add(item.FullName);
                }
                var f = Settings.Default.ExcludeFileList;
                f.Add("release.zip");
                Console.WriteLine("Starting release maker");
                foreach (string item in f)
                {
                    foreach (string item2 in files)
                    {
                        if (item2.Contains(item))
                        {
                            Console.WriteLine("INFO: Excluding \"" + item2 + "\" from the list");
                            files.Remove(item2);
                            break;
                        }
                    }
                }
                var releasezipname = "release.zip";
                var releasefile = Path.Combine(info.FullName, releasezipname);
                if (File.Exists(releasezipname))
                {
                    Console.WriteLine("Warning: Release file is exist");
                }
                Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile(releasefile);
                foreach (var file in files)
                {
                    if (zip.ContainsEntry(Path.GetFileName(file)))
                    {
                        zip.RemoveEntry(Path.GetFileName(file));
                        Console.WriteLine("Removing \"" + Path.GetFileName(file) + "\" from zip entry");
                    }
                    FileIOPermission parm = new FileIOPermission(FileIOPermissionAccess.AllAccess, file);
                    parm.AllLocalFiles = FileIOPermissionAccess.AllAccess;
                    try
                    {
                        parm.Demand();
                    }
                    catch (Exception sx)
                    {
                        LogAttrbute.log.LogFast(Common.Logging.Logging.LogFastInfo.Exception, sx.ToString());
                        Console.Error.WriteLine("Cannot add file name {0}, because you do not have the permission to read it, so this file is skip and not added to the zip file", file);
                        LogAttrbute.log.LogFast(Common.Logging.Logging.LogFastInfo.Error, string.Format("Cannot add file name {0}, because you do not have the permission to read it, so this file is skip and not added to the zip file", file));
                        continue;
                    }
                    zip.AddFile(file, "/");
                    Console.WriteLine("Adding \"" + Path.GetFileName(file) + "\" in the zip");
                }
                Console.WriteLine("Creating zip file...");
                try
                {
                    zip.Save();
                }
                catch (Exception sx)
                {
                    LogAttrbute.log.LogFast(Common.Logging.Logging.LogFastInfo.Exception, sx.ToString());
                    LogAttrbute.log.LogFast(Common.Logging.Logging.LogFastInfo.Error, "Cannot add the zip file into the directory because you do not have the permission to write the file");
                    Console.Error.WriteLine("Cannot add the zip file into the directory because you do not have the permission to write the file");
                    return;
                }
                Console.WriteLine("Creating zip file successful, produced file is located in \"" + releasefile + "\"");
            }
            Console.WriteLine();
        }
Exemplo n.º 5
0
        /// <summary>
        /// 解析指定压缩存档
        /// </summary>
        /// <param name="path">压缩存档路径</param>
        /// <returns></returns>
        public static SaveData Parse(string path)
        {
            SaveData ans = null;

            if (!path.EndsWith(".zip"))
            {
                if (File.Exists(Path.Combine(path, "date.json")))
                {
                    var content = File.ReadAllText(Path.Combine(path, "date.json"));
                    ans = JsonConvert.DeserializeObject(content, typeof(SaveData)) as SaveData;
                }
                else if (!File.Exists(Path.Combine(path, "TW_Save_Date_0.twV0")) &&
                         !File.Exists(Path.Combine(path, "TW_Save_Date_0.tw")))
                {
                    throw new System.Exception(path);
                }
                else
                {
                    string file     = null;
                    bool   rijndeal = true;
                    if (File.Exists(Path.Combine(path, "TW_Save_Date_0.twV0")))
                    {
                        file     = Path.Combine(path, "TW_Save_Date_0.twV0");
                        rijndeal = false;
                    }
                    else
                    {
                        file     = Path.Combine(path, "TW_Save_Date_0.tw");
                        rijndeal = true;
                    }
                    DateFile.SaveDate date = null;
                    try
                    {
                        date = typeof(SaveDateFile)
                               .GetMethod("GetData", BindingFlags.Public | BindingFlags.Instance)
                               .Invoke(SaveDateFile.instance,
                                       new object[] { file, typeof(DateFile.SaveDate), rijndeal })
                               as DateFile.SaveDate;
                    }
                    catch (AmbiguousMatchException)
                    {
                        date = typeof(SaveDateFile)
                               .GetMethod("GetData", BindingFlags.Public | BindingFlags.Instance)
                               .Invoke(SaveDateFile.instance,
                                       new object[] { file, typeof(DateFile.SaveDate) })
                               as DateFile.SaveDate;
                    }
                    ans = new SaveData(date._mainActorName, date._year, date._samsara,
                                       date._dayTrun, date._playerSeatName, date._playTime);
                    File.WriteAllText(Path.Combine(path, "date.json"),
                                      JsonConvert.SerializeObject(ans));
                }
            }
            else
            {
                using (var zip = new Ionic.Zip.ZipFile(path))
                {
                    if (zip.ContainsEntry("date.json"))
                    {
                        using (var stream = new MemoryStream())
                        {
                            zip.SelectEntries("date.json").First().Extract(stream);
                            stream.Seek(0, SeekOrigin.Begin);
                            using (var reader = new StreamReader(stream))
                            {
                                var serializer = JsonSerializer.Create();
                                ans = serializer.Deserialize(reader,
                                                             typeof(SaveData)) as SaveData;
                            }
                        }
                    }
                    else if (!zip.ContainsEntry("TW_Save_Date_0.twV0") &&
                             !zip.ContainsEntry("TW_Save_Date_0.tw"))
                    {
                        throw new System.Exception(path); // 错误存档
                    }
                    else // 不含加速文件
                    {
                        var tmp = Path.Combine(
                            System.Environment.GetEnvironmentVariable("TEMP"),
                            "SaveDate.tw");

                        if (File.Exists(tmp))
                        {
                            File.Delete(tmp);
                        }

                        bool rijndeal = true;
                        using (var stream = File.OpenWrite(tmp))
                        {
                            if (zip.ContainsEntry("TW_Save_Date_0.twV0"))
                            {
                                zip.SelectEntries("TW_Save_Date_0.twV0").First().Extract(stream);
                                rijndeal = false;
                            }
                            else if (zip.ContainsEntry("TW_Save_Date_0.tw"))
                            {
                                zip.SelectEntries("TW_Save_Date_0.tw").First().Extract(stream);
                                rijndeal = true;
                            }
                        }
                        DateFile.SaveDate date = null;
                        try
                        {
                            date = typeof(SaveDateFile)
                                   .GetMethod("GetData", BindingFlags.Public | BindingFlags.Instance)
                                   .Invoke(SaveDateFile.instance,
                                           new object[] { tmp, typeof(DateFile.SaveDate), rijndeal })
                                   as DateFile.SaveDate;
                        }
                        catch (AmbiguousMatchException)
                        {
                            date = typeof(SaveDateFile)
                                   .GetMethod("GetData", BindingFlags.Public | BindingFlags.Instance)
                                   .Invoke(SaveDateFile.instance,
                                           new object[] { tmp, typeof(DateFile.SaveDate) })
                                   as DateFile.SaveDate;
                        }
                        ans = new SaveData(date._mainActorName, date._year, date._samsara,
                                           date._dayTrun, date._playerSeatName, date._playTime);
                        //  添加加速文件
                        zip.AddEntry("date.json", JsonConvert.SerializeObject(ans));
                        zip.Save();
                    }
                }
            }
            return(ans);
        }