Exemplo n.º 1
0
        void Client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            this.BeginInvoke((MethodInvoker) delegate
            {
                DownloadProgress.Style = ProgressBarStyle.Marquee;

                string updatePath = Path.GetDirectoryName(Application.ExecutablePath) + "\\";
                using (ZipArchive archive = ZipFile.OpenRead(tempNameZip))
                {
                    int numFiles = archive.Entries.Count;
                    int current  = 1;

                    DownloadProgress.Style = ProgressBarStyle.Blocks;

                    foreach (ZipArchiveEntry entry in archive.Entries)
                    {
                        string fullName = entry.FullName;

                        if (fullName.Substring(fullName.Length - 1) == "/")
                        {
                            string folderName = fullName.Remove(fullName.Length - 1);
                            if (Directory.Exists(folderName))
                            {
                                Directory.Delete(folderName, true);
                            }

                            Directory.CreateDirectory(folderName);
                        }
                        else
                        {
                            if (fullName != "GameLauncherUpdater.exe")
                            {
                                if (File.Exists(fullName))
                                {
                                    File.Delete(fullName);
                                }

                                Information.Text = "Extracting: " + fullName;
                                try { entry.ExtractToFile(Path.Combine(updatePath, fullName)); } catch { }
                                Delay.WaitMSeconds(200);
                            }
                        }

                        DownloadProgress.Value = (int)((long)100 * current / numFiles);
                        current++;
                    }
                }

                Process.Start(@"GameLauncher.exe");
                error("Update completed. Starting GameLauncher.exe");
            });
        }
Exemplo n.º 2
0
        public static void SmartExtractFolder(string archive, string dest, bool overwrite = true)
        {
            var ext = Path.GetExtension(archive).ToLowerInvariant();

            if (ext == ".tar.gz" || ext == ".tgz")
            {
                ExtractTGZ(archive, dest);
            }
            else
            {
                ZipFile.ExtractToDirectory(archive, dest, overwrite);
            }
        }
Exemplo n.º 3
0
        public static void ZipTo(string archiveFilename, ZipFileInfo[] toArchiveFiles)
        {
            var zip = Zip.Open(archiveFilename, ZipArchiveMode.Create);

            foreach (var file in toArchiveFiles)
            {
                var filename = file.InArchiveFilename;
                if (String.IsNullOrEmpty(filename))
                {
                    filename = Path.GetFileName(file.SourceFilepath);
                }
                zip.CreateEntryFromFile(file.SourceFilepath, filename, CompressionLevel.Optimal);
            }
            // Dispose of the object when we are done
            zip.Dispose();
        }
Exemplo n.º 4
0
        /// <summary>
        /// 解压Zip文件
        /// </summary>
        /// <param name="zipFilePath"></param>
        /// <returns></returns>

        /*
         * public static ZipInfo UnZipFile(string zipFilePath)
         * {
         *  if (!File.Exists(zipFilePath))
         *  {
         *      return new ZipInfo
         *      {
         *          Success = false,
         *          InfoMessage = "没有找到解压文件"
         *      };
         *  }
         *  try
         *  {
         *      string path = zipFilePath.Replace(Path.GetExtension(zipFilePath), string.Empty) + "_" + System.DateTime.Now.ToString("yyyyMMddHHmmssfff");
         *      string directoryName = string.Empty;
         *      string fileName = string.Empty;
         *      using (ICSharpCode.SharpZipLib.Zip.ZipInputStream s = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(File.OpenRead(zipFilePath)))
         *      {
         *          ICSharpCode.SharpZipLib.Zip.ZipEntry theEntry;
         *          while ((theEntry = s.GetNextEntry()) != null)
         *          {
         *              directoryName = Path.GetDirectoryName(theEntry.Name);
         *              fileName = Path.GetFileName(theEntry.Name);
         *
         *              // create directory
         *              if (directoryName.Length > 0)
         *              {
         *                  directoryName = Path.Combine(path, directoryName);
         *                  if (!Directory.Exists(directoryName))
         *                      Directory.CreateDirectory(directoryName);
         *              }
         *              else
         *              {
         *                  directoryName = path;
         *              }
         *
         *              if (fileName != String.Empty)
         *              {
         *                  fileName = Path.Combine(directoryName, fileName);
         *                  using (FileStream streamWriter = File.Create(fileName))
         *                  {
         *                      int size = 2048;
         *                      byte[] data = new byte[2048];
         *                      while (true)
         *                      {
         *                          size = s.Read(data, 0, data.Length);
         *                          if (size > 0)
         *                          {
         *                              streamWriter.Write(data, 0, size);
         *                          }
         *                          else
         *                          {
         *                              break;
         *                          }
         *                      }
         *                  }
         *              }
         *          }
         *      }
         *      return new ZipInfo
         *      {
         *          Success = true,
         *          InfoMessage = "解压成功",
         *          UnZipPath=path
         *      };
         *  }
         *  catch (Exception ex)
         *  {
         *      return new ZipInfo
         *      {
         *          Success = false,
         *          InfoMessage = "解压文件:" + ex.Message
         *      };
         *  }
         * }
         */
        public static ZipInfo UnZipFile(string zipFilePath)
        {
            string path          = zipFilePath.Replace(Path.GetExtension(zipFilePath), string.Empty) + "_" + System.DateTime.Now.ToString("yyyyMMddHHmmssfff");
            string directoryName = string.Empty;
            string fileName      = string.Empty;

            using (ZipArchive zip = ZipFile.Open(Path.Combine(zipFilePath), ZipArchiveMode.Read))
            {
                foreach (ZipArchiveEntry entry in zip.Entries)
                {
                    directoryName = Path.GetDirectoryName(entry.Name);

                    // create directory
                    if (directoryName.Length > 0)
                    {
                        directoryName = Path.Combine(path, directoryName);
                        if (!System.IO.Directory.Exists(directoryName))
                        {
                            System.IO.Directory.CreateDirectory(directoryName);
                        }
                    }
                    else
                    {
                        directoryName = path;
                    }


                    entry.ExtractToFile(path);
                }
            }

            if (!File.Exists(zipFilePath))
            {
                return(new ZipInfo
                {
                    Success = false,
                    InfoMessage = "没有找到解压文件"
                });
            }
            return(new ZipInfo
            {
                Success = true,
                InfoMessage = "解压成功",
                UnZipPath = path
            });
        }
Exemplo n.º 5
0
        private static void Extract45Framework(string zipFile, string extractPath)
        {
            ZipArchive zipArchive = DotNetZipFile.OpenRead(zipFile);

            if (zipArchive.Entries != null && zipArchive.Entries.Count > 0)
            {
                Console.WriteLine("Extracting...");
                foreach (ZipArchiveEntry entry in zipArchive.Entries)
                {
                    try
                    {
                        if (string.IsNullOrEmpty(entry.Name))
                        {
                            // skip directory
                            continue;
                        }
                        string file = Path.Combine(extractPath, entry.FullName);
                        string path = Path.GetDirectoryName(file);
                        if (!Directory.Exists(path))
                        {
                            Directory.CreateDirectory(path);
                        }
                        Console.WriteLine(" - '" + entry.FullName + "'...");
                        if (File.Exists(file))
                        {
                            //Console.WriteLine("   - delete previous version...");
                            File.Delete(file);
                        }
                        entry.ExtractToFile(file);
                        long length = (new FileInfo(file)).Length;
                        if (entry.Length != length)
                        {
                            Console.WriteLine($"   - Failed to extract! Extracted only {length} out of {entry.Length} bytes");
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("   - Failed to extract: " + ex.Message);
                    }
                }
            }
        }
        private void generateWindowsPortable(Console console)
        {
            console.WriteLine("==================================");
            console.WriteLine("Generating Windows Portable Deploy...");
            var zipPath = textDeployFolder.Text + "\\" + textNamespace.Text + "." + textVersion.Text;

            if (chkDemo.Checked)
            {
                zipPath += "." + textDemoTag.Text;
            }
            zipPath += ".Windows.Portable.zip";
            var tempPath = textDeployFolder.Text + "\\" + textTitle.Text;

            var files = Directory.GetFiles(textReleaseFolder.Text, "*.*", SearchOption.AllDirectories);

            foreach (var file in files)
            {
                var fileRelative = file;
                fileRelative = fileRelative.Replace(textReleaseFolder.Text, "");
                var fileDir = new FileInfo(tempPath + fileRelative);
                fileDir.Directory.Create();

                File.Copy(file, tempPath + fileRelative, true);

                console.WriteLine("Copy To Temp: " + file);
            }
            console.WriteLine("Creating Zip File..." + zipPath);
            if (File.Exists(zipPath))
            {
                File.Delete(zipPath);
            }
            ZipFile.CreateFromDirectory(tempPath, zipPath, CompressionLevel.Optimal, true);
            console.Write("Deleting Temp Folder...");
            Directory.Delete(tempPath, true);
            console.WriteLine("OK");
        }
Exemplo n.º 7
0
        public string DownloadZip(string filesToDownload, string archName, string productId, string pathToFilesFolder)
        {
            List <string> files            = filesToDownload.Split(';').ToList();
            var           curDate          = DateTime.Now.ToShortDateString().Replace("-", "").Replace(":", "").Replace(".", "").Replace("\\", "").Replace("/", "");
            var           archiveOutFolder = AppDomain.CurrentDomain.BaseDirectory + ($"\\tempout\\{curDate}\\{productId}\\");
            var           tempFolder       = AppDomain.CurrentDomain.BaseDirectory + "\\temp\\";

            if (!Directory.Exists(archiveOutFolder))
            {
                Directory.CreateDirectory(archiveOutFolder);
            }
            var archive = string.Concat(archiveOutFolder, archName);

            ClearTempDirectories(curDate, tempFolder);
            files.ForEach(f => CopyFile(pathToFilesFolder, f, tempFolder));

            if (File.Exists(archive))
            {
                File.Delete(archive);
            }
            ZipFileCustom.CreateFromDirectory(tempFolder, archive, CompressionLevel.Optimal, false);

            return($"../tempout/{curDate}/{productId}/{archName}");
        }
Exemplo n.º 8
0
        internal static ZipEntry ReadDirEntry(ZipFile zipfile, Stream stream)
        {
            var reader = zipfile.CreateReader(stream);

            if (!reader.Expect(ZipConstants.ZipDirEntrySignature))
            {
                if (!reader.Expect(
                    ZipConstants.EndOfCentralDirectorySignature,
                    //ZipConstants.Zip64EndOfCentralDirectoryRecordSignature,
                    ZipConstants.ZipEntrySignature))
                {
                    throw new ZipException("0x{0:X8}处签名错误!", stream.Position);
                }
                return null;
            }

            var entry = reader.ReadObject<ZipEntry>();
            return entry;
        }
Exemplo n.º 9
0
        internal static ZipEntry ReadEntry(ZipFile zipfile, Stream stream, Boolean first, Boolean embedFileData)
        {
            var reader = zipfile.CreateReader(stream);
            // 读取文件头时忽略掉这些字段,这些都是DirEntry的字段
            reader.Settings.IgnoreMembers = dirMembers;

            // 有时候Zip文件以PK00开头
            if (first && reader.Expect(ZipConstants.PackedToRemovableMedia)) reader.ReadBytes(4);

            // 验证头部
            if (!reader.Expect(ZipConstants.ZipEntrySignature))
            {
                if (!reader.Expect(ZipConstants.ZipDirEntrySignature, ZipConstants.EndOfCentralDirectorySignature))
                    throw new ZipException("0x{0:X8}处签名错误!", stream.Position);

                return null;
            }

            var entry = reader.ReadObject<ZipEntry>();
            if (entry.IsDirectory) return entry;

            // 0长度的实体不要设置数据源
            if (entry.CompressedSize > 0)
            {
                // 是否内嵌文件数据
                entry.DataSource = embedFileData ? new ArrayDataSource(stream, (Int32)entry.CompressedSize) : new StreamDataSource(stream, stream.Position, 0);
                entry.DataSource.IsCompressed = entry.CompressionMethod != CompressionMethod.Stored;

                // 移到文件数据之后,可能是文件头
                if (!embedFileData) stream.Seek(entry.CompressedSize, SeekOrigin.Current);
            }

            // 如果有扩展,则跳过
            if (entry.BitField.Has(GeneralBitFlags.Descriptor))
            {
                //stream.Seek(20, SeekOrigin.Current);

                // 在某些只读流中,可能无法回头设置校验和大小,此时可通过描述符在文件内容之后设置
                entry.Crc = reader.ReadUInt32();
                entry.CompressedSize = reader.ReadUInt32();
                entry.UncompressedSize = reader.ReadUInt32();
            }

            return entry;
        }
Exemplo n.º 10
0
 /// <summary>
 /// Method that will extract from file all files.
 /// </summary>
 /// <param name="zipFilePath">zip file to extract files from</param>
 /// <param name="destinationPath">destination path to extract files to</param>
 /// <returns>
 /// returns the file path of all the files extracted from zip.
 /// </returns>
 private string[] UnzipAllFiles(string zipFilePath, string destinationPath)
 {
     ZipFile.ExtractToDirectory(zipFilePath, destinationPath);
     return(Directory.GetFiles(destinationPath));
 }