Exemple #1
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();
            }
        }