public List <string> ProcessArchive(string archivePath, string pathToExtract) { List <string> exrtactedFiles = new List <string>(); if (!RarArchive.IsRarFile(archivePath) && !ZipFile.IsZipFile(archivePath)) { return(null); } if (ZipFile.IsZipFile(archivePath)) { exrtactedFiles = ProcessZipArchive(archivePath, pathToExtract); } if (RarArchive.IsRarFile(archivePath)) { ProcessRarArchive(archivePath, pathToExtract); } return(exrtactedFiles); }
/// <summary> /// Checks a file to see if it is a valid zip file. /// </summary> /// /// <remarks> /// <para> /// This method opens the specified zip file, reads in the zip archive, /// verifying the ZIP metadata as it reads. /// </para> /// /// <para> /// If everything succeeds, then the method returns true. If anything fails - /// for example if an incorrect signature or CRC is found, indicating a /// corrupt file, the the method returns false. This method also returns /// false for a file that does not exist. /// </para> /// /// <para> /// If <paramref name="testExtract"/> is true, as part of its check, this /// method reads in the content for each entry, expands it, and checks CRCs. /// This provides an additional check beyond verifying the zip header and /// directory data. /// </para> /// /// <para> /// If <paramref name="testExtract"/> is true, and if any of the zip entries /// are protected with a password, this method will return false. If you want /// to verify a <c>ZipFile</c> that has entries which are protected with a /// password, you will need to do that manually. /// </para> /// /// </remarks> /// /// <param name="fileName">The zip file to check.</param> /// <param name="testExtract">true if the caller wants to extract each entry.</param> /// <returns>true if the file contains a valid zip file.</returns> internal static bool IsZipFile(string fileName, bool testExtract) { bool result = false; try { string fullPath = GetFullPath(fileName); var file = FileSystem.Current.GetFileFromPathAsync(fullPath).ExecuteSync(); if (file == null) { return(false); } using (var s = file.OpenAsync(FileAccess.Read).ExecuteSync()) { result = ZipFile.IsZipFile(s, testExtract); } } catch (IOException) { } catch (ZipException) { } return(result); }
/// <summary> /// A wrapper for <see cref="ZipFile.IsZipFile(string, bool)">ZipFile.IsZipFile(string, bool)</see> /// </summary> /// <remarks> /// We cannot use "overloaded" Method names in COM interop. /// So, here, we use a unique name. /// </remarks> /// <param name="filename">The filename to of the zip file to check.</param> /// <returns>true if the file contains a valid zip file.</returns> public bool IsZipFileWithExtract(string filename) { return(ZipFile.IsZipFile(filename, true)); }
/// <summary> /// A wrapper for <see cref="ZipFile.IsZipFile(string)">ZipFile.IsZipFile(string)</see> /// </summary> /// <param name="filename">The filename to of the zip file to check.</param> /// <returns>true if the file contains a valid zip file.</returns> public bool IsZipFile(string filename) { return(ZipFile.IsZipFile(filename)); }
/// <summary> /// Checks the given file to see if it appears to be a valid zip file. /// </summary> /// <remarks> /// /// <para> /// Calling this method is equivalent to calling <see cref="IsZipFile(string, /// bool)"/> with the testExtract parameter set to false. /// </para> /// </remarks> /// /// <param name="stream">The stream to check.</param> /// <returns>true if the stream appears to be a zip archive.</returns> public static bool IsZipFile(Stream stream) { return(ZipFile.IsZipFile(stream)); }
/// <summary> /// Checks a stream to see if it contains a valid zip archive. /// </summary> /// /// <remarks> /// <para> /// This method reads the zip archive contained in the specified stream, verifying /// the ZIP metadata as it reads. If testExtract is true, this method also extracts /// each entry in the archive, dumping all the bits into <see cref="Stream.Null"/>. /// </para> /// /// <para> /// If everything succeeds, then the method returns true. If anything fails - /// for example if an incorrect signature or CRC is found, indicating a corrupt /// file, the the method returns false. This method also returns false for a /// file that does not exist. /// </para> /// /// <para> /// If <c>testExtract</c> is true, this method reads in the content for each /// entry, expands it, and checks CRCs. This provides an additional check /// beyond verifying the zip header data. /// </para> /// /// <para> /// If <c>testExtract</c> is true, and if any of the zip entries are protected /// with a password, this method will return false. If you want to verify a /// ZipFile that has entries which are protected with a password, you will need /// to do that manually. /// </para> /// </remarks> /// /// <seealso cref="IsZipFile(string, bool)"/> /// /// <param name="stream">The stream to check.</param> /// <param name="testExtract">true if the caller wants to extract each entry.</param> /// <returns>true if the stream contains a valid zip archive.</returns> public static bool IsZipFile(Stream stream, bool testExtract) { return(ZipFile.IsZipFile(stream, testExtract)); }