Exemplo n.º 1
0
        public static Exception UnzipFile(string zipFile, string outputDirectory, UnzipOptions options)
        {
            options = options ?? new UnzipOptions();
            try
            {
                var root = new DirectoryInfo(outputDirectory);
                root.Create();
                var rootPath = root.FullName + "/";
                using (ZipFile zip = ZipFile.Read(zipFile))
                {
                    zip.ExtractAll(outputDirectory, ExtractExistingFileAction.OverwriteSilently);
                }

                /*
                 *              using (var zip = (IDisposable) ZipArchive_OpenOnFile.Invoke(null, new object[] {zipFile, FileMode.Open, FileAccess.Read, FileShare.Read, false}))
                 *              {
                 *                      var ioManager = ZipArchive_ZipIOBlockManager.GetValue(zip);
                 *                      ZipIOBlockManager_Encoding.SetValue(ioManager, new WarpedEncoding(options.Encoding ?? Encoding.Default));
                 *
                 *                      var files = (IEnumerable) ZipArchive_GetFiles.Invoke(zip, new object[] {});
                 *                      IEnumerable<string> exclude = (options.Exclude ?? new List<string>());
                 *                      if (exclude.Count() > 1000)
                 *                      {
                 *                              exclude = exclude.AsParallel();
                 *                      }
                 *
                 *                      foreach (var item in files)
                 *                      {
                 *                              var name = (string) ZipFileInfo_Name.GetValue(item, null);
                 *                              if (exclude.Any(name.StartsWith))
                 *                              {
                 *                                      continue;
                 *                              }
                 *                              if ((bool) ZipFileInfo_FolderFlag.GetValue(item, null) || name.Last() == '/')
                 *      {
                 *                                      Directory.CreateDirectory(rootPath + name);
                 *                                      continue;
                 *                              }
                 *                              using (var stream = (Stream) ZipFileInfo_GetStream.Invoke(item, new object[] {FileMode.Open, FileAccess.Read}))
                 *                              {
                 *                                      var filePath = rootPath + name;
                 *                                      var directoryInfo = new FileInfo(filePath).Directory;
                 *                                      if (directoryInfo != null) directoryInfo.Create();
                 *                                      using (var fs = new FileStream(filePath, FileMode.Create))
                 *                                      {
                 *                                              stream.CopyTo(fs);
                 *                                      }
                 *                              }
                 *                      }
                 *              }
                 */
                return(null);
            }
            catch (Exception exp)
            {
                return(exp);
            }
        }
Exemplo n.º 2
0
		public static Exception UnzipFile(string zipFile, string outputDirectory, UnzipOptions options)
		{
			options = options ?? new UnzipOptions();
			try
			{
				var root = new DirectoryInfo(outputDirectory);
				root.Create();
				var rootPath = root.FullName + "/";
				using (var zip = (IDisposable) ZipArchive_OpenOnFile.Invoke(null, new object[] {zipFile, FileMode.Open, FileAccess.Read, FileShare.Read, false}))
				{
					var ioManager = ZipArchive_ZipIOBlockManager.GetValue(zip);
					ZipIOBlockManager_Encoding.SetValue(ioManager, new WarpedEncoding(options.Encoding ?? Encoding.Default));

					var files = (IEnumerable) ZipArchive_GetFiles.Invoke(zip, new object[] {});
					IEnumerable<string> exclude = (options.Exclude ?? new List<string>());
					if (exclude.Count() > 1000)
					{
						exclude = exclude.AsParallel();
					}

					foreach (var item in files)
					{
						var name = (string) ZipFileInfo_Name.GetValue(item, null);
						if (exclude.Any(name.StartsWith))
						{
							continue;
						}
						if ((bool) ZipFileInfo_FolderFlag.GetValue(item, null))
						{
							Directory.CreateDirectory(rootPath + name);
							continue;
						}
						using (var stream = (Stream) ZipFileInfo_GetStream.Invoke(item, new object[] {FileMode.Open, FileAccess.Read}))
						{
							var filePath = rootPath + name;
							var directoryInfo = new FileInfo(filePath).Directory;
							if (directoryInfo != null) directoryInfo.Create();
							using (var fs = new FileStream(filePath, FileMode.Create))
							{
								stream.CopyTo(fs);
							}
						}
					}
				}
				return null;
			}
			catch (Exception exp)
			{
				return exp;
			}
		}
Exemplo n.º 3
0
        public static Exception UnzipFile(string zipFile, string outputDirectory, UnzipOptions options)
        {
            options = options ?? new UnzipOptions();
            try
            {
                var root = new DirectoryInfo(outputDirectory);
                root.Create();
                var rootPath = root.FullName + "/";

                using (var zip = ZipFile.Open(zipFile, ZipArchiveMode.Read, options.Encoding ?? Encoding.Default))
                {
                    var files = zip.Entries;
                    IEnumerable <string> exclude = (options.Exclude ?? new List <string>());
                    if (exclude.Count() > 1000)
                    {
                        exclude = exclude.AsParallel();
                    }

                    foreach (var item in files)
                    {
                        var name = item.FullName;
                        if (exclude.Any(name.StartsWith))
                        {
                            continue;
                        }

                        // Determine whether the entry is a directory
                        if (name.Last() == '/')
                        {
                            Directory.CreateDirectory(rootPath + name);
                            continue;
                        }

                        var filePath = rootPath + name;
                        item.ExtractToFile(filePath, true);
                    }
                }
                return(null);
            }
            catch (Exception exp)
            {
                return(exp);
            }
        }
Exemplo n.º 4
0
		public static bool Unzip(string zipFile, string outputDirectory, UnzipOptions options)
		{
			return UnzipFile(zipFile, outputDirectory, options) == null;
		}
Exemplo n.º 5
0
 public static bool Unzip(string zipFile, string outputDirectory, UnzipOptions options)
 {
     return(UnzipFile(zipFile, outputDirectory, options) == null);
 }