Exemple #1
0
		/// <summary>
		/// Remove password from zip file
		/// </summary>
		/// <param name="fileName"></param>
		/// <param name="password"></param>
		/// <returns></returns>
		static public bool RemovePasswordFromZip(string fileName, string password)
		{
			// Poor mans version, extract entire zip and then read into a new one, but handles encrypted zips with empty folders in
			string tempDump = string.Empty;
			try
			{
				using (ZipWrapper zip = new ZipWrapper())
				{
					zip.DecryptPassword = password;
					zip.OpenZip(fileName);

					tempDump = Path.Combine(Path.GetDirectoryName(fileName), Guid.NewGuid().ToString());
					if (!zip.Extract(tempDump))
					{
						Logger.LogError(zip.LastErrorText);
						throw new Exception(string.Format("Unable to extract {0] to {1}", fileName, tempDump));
					}
					zip.CloseZip();
				}

				System.IO.File.Delete(fileName); // Remove the original encrypted file

				// Now create a new zip without a password
				using (ZipWrapper zipDecrypted = new ZipWrapper())
				{
					zipDecrypted.AppendFiles(tempDump, true);
					zipDecrypted.FileName = fileName;
					if (!zipDecrypted.WriteZipAndClose()) // Create a new decrypted zip
					{
						Logger.LogError(zipDecrypted.LastErrorText);
						throw new Exception("Failed trying to create decrypted version of: " + fileName);
					}
				}
			}
			finally
			{
			    ForceDeleteDirectory(tempDump);
			}

			return true;



			//// Chilkat seems to have an issue with passworded zips containing empty folders, once password is removed(using below procedure that they outline in their samples
			//// http://www.example-code.com/csharp/zip_RemoveZipEncryption.asp) errors occur when trying to use the decrypted zip
			////
			//// Also it seems to have issues with complex nesting, missing/not seeing files below the root after decryption
			//using (Chilkat.Zip zip = new Chilkat.Zip())
			//{
			//    zip.UnlockComponent(Chilkat_Serial);

			//    zip.DecryptPassword = password;
			//    if (!zip.OpenZip(fileName))
			//    {
			//        Logger.LogDebug("Unable to open: " + fileName);
			//        Logger.LogDebug(zip.LastErrorText);
			//        return false;
			//    }

			//    zip.Encryption = 0;
			//    zip.PasswordProtect = false;

			//    string tmpFile = Path.Combine(Path.GetDirectoryName(fileName), Guid.NewGuid().ToString() + ".zip");
			//    zip.FileName = tmpFile;

			//    // From Chilkat::
			//    //  Write the unencrypted .zip
			//    //  What happens during WriteZipAndClose?  --
			//    //  The encrypted entries from myEncrypted.zip are streamed in,
			//    //  decrypted, and then written out directly into unencrypted.zip
			//    //  In other words, internally the component is smart enough
			//    //  to stream the data from the existing .zip to the new .zip
			//    //  automatically, decrypting in the process..
			//    if (zip.WriteZipAndClose())
			//    {
			//        Logger.LogInfo("Saved unpassworded zip to: " + tmpFile);
			//        System.IO.File.Copy(tmpFile, fileName, true);
			//        Logger.LogInfo("Copied: " + tmpFile + " back to: " + fileName);

			//        System.IO.File.Delete(tmpFile);
			//        return true;
			//    }
			//    else
			//    {
			//        Logger.LogDebug("Unable to decrypt: " + fileName);
			//        Logger.LogDebug(zip.LastErrorText);
			//        return false;
			//    }
			//}			
		}