Ejemplo n.º 1
0
		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);
					}
				}
			}
		}
Ejemplo n.º 2
0
		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);
			}
		}
Ejemplo n.º 3
0
        static public bool VerifyPassword(string filePath, string password)
        {
            try
            {
                using (var 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
                    if (zip.PasswordProtect || (zip.Encryption > 0))
                    {
                        zip.SetPassword(password);
                        return zip.VerifyPassword();
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                Logger.LogDebug(ex);
                throw new Exception("Unable to process zip file:" + filePath);
            }
        }
Ejemplo n.º 4
0
        public bool PackContainer(Collection<IContainer> fileList, FileData filedata, Dictionary<string, string> properties, string baseFileName, string sPassword)
        {
            filedata.Password = sPassword;

            string outputFileName = Path.GetTempFileName();

            try
            {
				using (ZipWrapper zip = new ZipWrapper())
                {
                    zip.SetCompressionLevel(9); // max compression
                    zip.FileName = outputFileName;
                    zip.TempDir = Path.GetDirectoryName(outputFileName);
					//  To zip using utf-8 filenames, set the OemCodePage = 65001
                    zip.OemCodePage = 65001;

                    if (filedata.Password != null && 0 < filedata.Password.Length)
                    {
                        string sEncryption = string.Empty;
                        string sEncryptionKeyLength = string.Empty;
                        zip.Encryption = 0;

                        if (properties.TryGetValue("Encryption", out sEncryption))
                        {
                            try
                            {
                                zip.EncryptKeyLength = 128;

                                if (properties.TryGetValue("EncryptionKeyLength", out sEncryptionKeyLength))
                                    zip.EncryptKeyLength = System.Convert.ToInt32(sEncryptionKeyLength);

                                zip.Encryption = System.Convert.ToInt32(sEncryption);
                            }
                            catch (FormatException)
                            {
                                zip.Encryption = 0;
                                zip.EncryptKeyLength = 0;
                            }
                        }

                        if (zip.Encryption == 0)
                            zip.PasswordProtect = true;

                        zip.SetPassword(filedata.Password);
                    }

					// Get the folders in, first 
					for (int i = 0; i < fileList.Count; ++i)
					{
						IFile zipEntry = fileList[i] as IFile;
						if (null == zipEntry)
						{
							Logger.LogError("Unable to convert IContainer to IFile file because underlying type was " + fileList[i].GetType());
							continue;
						}

						if (zipEntry.FileType == FileType.Folder) // Just create the folder
						{
							zip.AppendOneFileOrDir(zipEntry.DisplayName, false);
						}
					}


					// And then the files
                    for (int i = 0; i < fileList.Count; ++i)
                    {
						IFile zipEntry = fileList[i] as IFile;
						if (null == zipEntry)
                        {
							Logger.LogError("Unable to convert IContainer to IFile file because underlying type was " + fileList[i].GetType());
                          	continue;
                        }

						if (zipEntry.FileType == FileType.Folder) // Skip the folders as we have already created them.
						{
							continue;
						}
							
                        // Which parts of the path do we NOT want to put into the zip folder structure
                        string ignorePath = zipEntry.FileName.ToLower().Replace(zipEntry.DisplayName.ToLower(), "");

                        // This is needed if you right click on a unicode .docx file name -> Send to Mail Recipient
                        // because outlook converts it into 8.3 file format filename so the display name is different from the file name.
						string displayName = Path.Combine(Path.GetDirectoryName(zipEntry.DisplayName), Path.GetFileName(zipEntry.FileName));

                        if (!ignorePath.EndsWith("\\"))
                        {
                            ignorePath = ignorePath.Substring(0, ignorePath.LastIndexOf("\\") + 1);
                        }

                        zip.AppendFromDir = ignorePath;
                        zip.AppendOneFileOrDir(displayName, true /*We want to recreate folders within the zip files*/);

                    }
					
					if (!zip.WriteZipAndClose())
					{
						Logger.LogError(zip.LastErrorText);
						throw new Exception(String.Format("Failed trying pack container {0}, temp version of {1} ", outputFileName, baseFileName));
					}

					// Now replace the orig zip with our new temp one
					System.IO.File.Copy(outputFileName, baseFileName, true);
                }

                filedata.BinaryFileData = new OptimizedBinaryData(outputFileName);
                return true;
            }
			catch (Exception ex)
			{
				Logger.LogError(ex.Message);
				throw ex;
			}
            finally
            {
				// Delete the temp zip we built
                System.IO.File.Delete(outputFileName);

				// Give the Chilkat zip object chance to finish with the files, otherwise we can get a locked file exception later when we try to delete the temp files
				System.GC.Collect();
				System.GC.WaitForPendingFinalizers();
            }
        }