static public List<string> GetFileNames(string fileName, bool includeDirectorys) { Logger.LogInfo("Getting filesnames within: " + fileName); List<string> fileNames = new List<string>(); using (ZipWrapper zip = new ZipWrapper()) { if (!zip.OpenZip(fileName)) { Logger.LogDebug("Unable to open: " + fileName); throw new Exception("Unable to open Zip file:" + fileName); } int n = zip.NumEntries; Logger.LogInfo(string.Format("Zip contains [{0}] files", n.ToString())); Chilkat.ZipEntry entry; for (int i = 0; i <= n - 1; i++) { entry = zip.GetEntryByIndex(i); if (entry.IsDirectory && !includeDirectorys)//Skip over directories { continue; } fileNames.Add(entry.FileName); Logger.LogInfo("Zip contains file: " + entry.FileName); } } return fileNames; }
public ZipOpener(BinaryData inputData, string password) { m_zip = new ZipWrapper(); m_zip.SetPassword(password); if (inputData.Length >= 85000) { m_fileName = Path.GetTempFileName(); inputData.Save(m_fileName); if (!m_zip.OpenZip(m_fileName)) { throw new System.Exception(m_zip.LastErrorText); } } else { using (Stream str = inputData.AsStream()) { byte[] data = StreamUtils.SafeButUnpleasantReadAllStreamIntoByteArray(str); if (!m_zip.OpenFromMemory(data)) { throw new System.Exception(m_zip.LastErrorText); } } } }
public ZipOpener(string filePath, string password) { m_zip = new ZipWrapper(); m_zip.SetPassword(password); //m_fileName = filePath; DON'T DO THIS! would result in the file being DELETED on Dispose(), not what we want if (!m_zip.OpenZip(filePath)) { throw new System.Exception(m_zip.LastErrorText); } }
/// <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; // } //} }
static public bool ZipHasPassword(string filePath) { try { using (ZipWrapper zip = new ZipWrapper()) { zip.OpenZip(filePath); Logger.LogInfo(string.Format("File {0} PasswordProtect status: {1}", filePath, zip.PasswordProtect.ToString())); Logger.LogInfo(string.Format("File {0} Encryption value: {1}", filePath, zip.Encryption.ToString())); // PasswordProtect == old style zip 2.0 encryption // Encryption > 0 indicates 'modern' strong encryption has been used return zip.PasswordProtect || (zip.Encryption > 0); } } catch (Exception ex) { Logger.LogDebug(ex); throw new Exception("Unable to process zip file:" + filePath); } }
/// <summary> /// Assumes no inital password /// </summary> /// <param name="fileName"></param> /// <param name="password"></param> /// <param name="encryption"></param> /// <param name="encryptionKeyLength"></param> /// <returns></returns> static public bool AddPasswordToZip(string fileName, string password, string encryption, string encryptionKeyLength) { if (VerifyPassword(fileName, password)) { return true; } using(var zip = new ZipWrapper()) { zip.OpenZip(fileName); zip.Encryption = 0; if (!string.IsNullOrEmpty(encryption)) // 'modern' zip encryption being used { try { zip.EncryptKeyLength = 128; if (!string.IsNullOrEmpty(encryptionKeyLength)) { zip.EncryptKeyLength = System.Convert.ToInt32(encryptionKeyLength); } zip.Encryption = System.Convert.ToInt32(encryption); } catch (FormatException) { zip.Encryption = 0; zip.EncryptKeyLength = 0; } } if (zip.Encryption == 0) // zip 2.0 encryption { zip.PasswordProtect = true; } zip.EncryptPassword = password; if (!zip.WriteZipAndClose()) { Logger.LogError(zip.LastErrorText); throw new Exception(String.Format("Failed adding password to zip {0}", fileName)); } return true; } }