Exemplo n.º 1
0
        private string ZipperExtractFileToPath(string destRootPath, System.IO.Compression.ZipArchiveEntry item)
        {
            var destFilename = CleanFilenameString(System.IO.Path.Combine(destRootPath, item.Name));

            FileStorageHelper.DeleteFile(destFilename);
            FileStorageHelper.CreateDirectory(System.IO.Path.GetDirectoryName(destFilename));
            //
            var count = 5;

            do
            {
                try
                {
                    item.ExtractToFile(destFilename, true);
                    if (System.IO.File.Exists(destFilename))
                    {
                        var fileInfo = new System.IO.FileInfo(destFilename);
                        fileInfo.LastWriteTime = DateTime.Now;
                    }
                    break;
                }
                catch
                {
                    if (--count <= 0)
                    {
                        throw;
                    }
                    Threading.ThreadingHelper.Sleep(200);
                }
            } while (true);
            //
            return(destFilename);
        }
Exemplo n.º 2
0
        internal static void ExtractRelativeToDirectory(this ZipArchiveEntry source, string destinationDirectoryName, bool overwrite)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (destinationDirectoryName == null)
            {
                throw new ArgumentNullException(nameof(destinationDirectoryName));
            }

            // Note that this will give us a good DirectoryInfo even if destinationDirectoryName exists:
            DirectoryInfo di = Directory.CreateDirectory(destinationDirectoryName);
            string        destinationDirectoryFullPath = di.FullName;

            if (!destinationDirectoryFullPath.EndsWith(Path.DirectorySeparatorChar))
            {
                destinationDirectoryFullPath += Path.DirectorySeparatorChar;
            }

            string fileDestinationPath = Path.GetFullPath(Path.Combine(destinationDirectoryFullPath, source.FullName));

            if (!fileDestinationPath.StartsWith(destinationDirectoryFullPath, PathInternal.StringComparison))
            {
                throw new IOException(SR.IO_ExtractingResultsInOutside);
            }

            if (Path.GetFileName(fileDestinationPath).Length == 0)
            {
                // If it is a directory:

                if (source.Length != 0)
                {
                    throw new IOException(SR.IO_DirectoryNameWithData);
                }

                Directory.CreateDirectory(fileDestinationPath);
            }
            else
            {
                // If it is a file:
                // Create containing directory:
                Directory.CreateDirectory(Path.GetDirectoryName(fileDestinationPath) !);
                source.ExtractToFile(fileDestinationPath, overwrite: overwrite);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Safely extracts a single file from a zip file
        /// </summary>
        /// <param name="file">
        /// The zip entry we are pulling the file from
        /// </param>
        /// <param name="destinationPath">
        /// The root of where the file is going
        /// </param>
        /// <param name="overwriteMethod">
        /// Specifies how we are going to handle an existing file.
        /// The default is Overwrite.IfNewer.
        /// </param>
        public static void ImprovedExtractToFile(ZipArchiveEntry file,
                                                 string destinationPath,
                                                 Overwrite overwriteMethod = Overwrite.IfNewer)
        {
            //Gets the complete path for the destination file, including any
            //relative paths that were in the zip file
            string destinationFileName = Path.Combine(destinationPath, file.FullName);

            //Gets just the new path, minus the file name so we can create the
            //directory if it does not exist
            string destinationFilePath = Path.GetDirectoryName(destinationFileName);

            //Creates the directory (if it doesn't exist) for the new path
            Directory.CreateDirectory(destinationFilePath);

            //Determines what to do with the file based upon the
            //method of overwriting chosen
            switch (overwriteMethod)
            {
                case Overwrite.Always:
                    //Just put the file in and overwrite anything that is found
                    file.ExtractToFile(destinationFileName, true);
                    break;
                case Overwrite.IfNewer:
                    //Checks to see if the file exists, and if so, if it should
                    //be overwritten
                    if (!File.Exists(destinationFileName) || File.GetLastWriteTime(destinationFileName) < file.LastWriteTime)
                    {
                        //Either the file didn't exist or this file is newer, so
                        //we will extract it and overwrite any existing file
                        file.ExtractToFile(destinationFileName, true);
                    }
                    break;
                case Overwrite.Never:
                    //Put the file in if it is new but ignores the 
                    //file if it already exists
                    if (!File.Exists(destinationFileName))
                    {
                        file.ExtractToFile(destinationFileName);
                    }
                    break;
                default:
                    break;
            }
        }
Exemplo n.º 4
0
		private void ExtractEntry(ZipArchiveEntry entry, string destinationPath, bool overwrite = false) {
			string destinationFolder = Path.GetDirectoryName(destinationPath);
			if(destinationFolder != null && !Directory.Exists(destinationFolder)) Directory.CreateDirectory(destinationFolder);
			if(overwrite || !File.Exists(destinationPath)) entry.ExtractToFile(destinationPath, true);
		}
Exemplo n.º 5
0
 public void ExtractToFile(ZipArchiveEntry source, string destinationFileName, bool overwrite)
 {
     source.ExtractToFile(destinationFileName, overwrite);
 }
Exemplo n.º 6
0
 private string UnpackAndGetFileName(ZipArchiveEntry entry, string destinationDirectory)
 {
     string pathToUnpackedFile = Path.Combine(destinationDirectory, entry.Name);
     entry.ExtractToFile(pathToUnpackedFile);
     return pathToUnpackedFile;
 }
Exemplo n.º 7
0
 public static void ExtractToFile(this ZipArchiveEntry source, string destinationFileName)
 {
     source.ExtractToFile(destinationFileName, false);
 }