Esempio n. 1
0
		/// <summary>
		/// Compress multiple items with the ZIP algorithm.
		/// </summary>
		/// <param name="infos">The items to compress.</param>
		/// <param name="progress1">The progress1.</param>
		/// <param name="progress2">The progress2.</param>
		/// <param name="destinationStream">The destination stream.</param>
		/// <returns></returns>
		private static Stream CompressHeterogenousToStream(
			CompressHeterogenousInfos infos,
			ICancelableProgress progress1,
			Cancelable progress2,
			Stream destinationStream )
		{
			if ( infos == null ||
				infos.InternalItems == null ||
				infos.InternalItems.Count <= 0 )
			{
				return null;
			}
			else
			{
				// No "using" block.
				ZipOutputStream zip = new ZipOutputStream( destinationStream );

				Crc32 crc = new Crc32();
				zip.SetLevel( 9 );	// 0..9.

				int index = 1;
				foreach ( CompressHeterogenousInfo info
					in infos.InternalItems )
				{
					// Cancel, if requested.
					if ( progress1 != null )
					{
						if ( progress1.OnProgress( null, EventArgs.Empty ) ==
							CancelMode.Cancel )
						{
							return null;
						}
					}
					// Cancel, if requested.
					if ( progress2 != null )
					{
						if ( progress2( info ) == CancelMode.Cancel )
						{
							return null;
						}
					}

					// --

					byte[] buffer = null;
					string fileName = null;
					bool putEntry;

					switch ( info.Type )
					{
						case CompressHeterogenousInfo.InfoType.String:
							fileName = info.FilePath;
							if ( fileName == null || fileName.Length <= 0 )
							{
								fileName =
									string.Format(
									@"file{0}.bin",
									index );
							}

							buffer = Encoding.UTF8.GetBytes( info.Content );

							putEntry = true;
							break;

						case CompressHeterogenousInfo.InfoType.File:
							fileName = Path.GetFileName( info.FilePath );

							ZipEntry entry = new ZipEntry( fileName );
							zip.PutNextEntry( entry );

							using ( FileStream fs = new FileStream(
								info.FilePath,
								FileMode.Open,
								FileAccess.Read,
								FileShare.Read ) )
							using ( BinaryReader r = new BinaryReader( fs ) )
							{
								byte[] smallBuffer = new byte[16384];

								int bytesRead;
								do
								{
									bytesRead = r.Read(
										smallBuffer,
										0,
										smallBuffer.Length );
									zip.Write( smallBuffer, 0, bytesRead );

									// --

									// Cancel, if requested.
									if ( progress1 != null )
									{
										if ( progress1.OnProgress(
											null,
											EventArgs.Empty ) ==
											CancelMode.Cancel )
										{
											return null;
										}
									}
									// Cancel, if requested.
									if ( progress2 != null )
									{
										if ( progress2( info ) ==
											CancelMode.Cancel )
										{
											return null;
										}
									}
								}
								while ( bytesRead > 0 );
							}

							putEntry = false;
							break;

						case CompressHeterogenousInfo.InfoType.Bytes:
							fileName = info.FilePath;
							if ( fileName == null ||
								fileName.Length <= 0 )
							{
								fileName =
									string.Format(
									@"file{0}.bin",
									index );
							}

							buffer = info.Bytes;

							putEntry = true;
							break;

						default:
							Debug.Assert(
								false,
								string.Format(
								@"Unknown compression info type '{0}'.",
								info.Type ) );
							putEntry = false;
							break;
					}

					// --

					if ( putEntry )
					{
						ZipEntry entry = new ZipEntry( fileName );
						entry.DateTime = DateTime.Now;
						entry.Size = buffer.Length;

						crc.Reset();
						crc.Update( buffer );

						entry.Crc = crc.Value;

						zip.PutNextEntry( entry );
						zip.Write( buffer, 0, buffer.Length );
					}

					index++;
				}

				zip.Finish();
				return zip;
			}
		}
		/// <summary>
		/// Compress multiple items with the ZIP algorithm.
		/// </summary>
		/// <param name="infos">The items to compress.</param>
		/// <returns>Returns the compressed items.</returns>
		public static byte[] CompressHeterogenous(
			CompressHeterogenousInfos infos )
		{
			if ( infos == null ||
				infos.InternalItems == null ||
				infos.InternalItems.Count <= 0 )
			{
				return null;
			}
			else
			{
				using ( MemoryStream buf = new MemoryStream() )
				using ( ZipOutputStream zip = new ZipOutputStream( buf ) )
				{
					Crc32 crc = new Crc32();
					zip.SetLevel( 9 );	// 0..9.

					int index = 1;
					foreach ( CompressHeterogenousInfo info in infos.InternalItems )
					{
						byte[] buffer = null;
						string fileName = null;

						switch ( info.Type )
						{
							case CompressHeterogenousInfo.InfoType.String:
								{
									fileName = info.FilePath;
									if ( fileName == null || fileName.Length <= 0 )
									{
										fileName =
											string.Format(
											@"file{0}.bin",
											index );
									}

									buffer = Encoding.UTF8.GetBytes( info.Content );
								}
								break;

							case CompressHeterogenousInfo.InfoType.File:
								{
									using ( FileStream fs = new FileStream(
										info.FilePath,
										FileMode.Open,
										FileAccess.Read,
										FileShare.Read ) )
									using ( BinaryReader r = new BinaryReader( fs ) )
									{
										buffer = new byte[fs.Length];
										fs.Read( buffer, 0, buffer.Length );

										fileName = Path.GetFileName( info.FilePath );
									}
								}
								break;

							case CompressHeterogenousInfo.InfoType.Bytes:
								{
									fileName = info.FilePath;
									if ( fileName == null ||
										fileName.Length <= 0 )
									{
										fileName =
											string.Format(
											@"file{0}.bin",
											index );
									}

									buffer = info.Bytes;
								}
								break;

							default:
								Debug.Assert(
									false,
									string.Format(
									@"Unknown compression info type '{0}'.",
									info.Type ) );
								break;
						}

						ZipEntry entry = new ZipEntry( fileName );
						entry.DateTime = DateTime.Now;
						entry.Size = buffer.Length;

						crc.Reset();
						crc.Update( buffer );

						entry.Crc = crc.Value;

						zip.PutNextEntry( entry );
						zip.Write( buffer, 0, buffer.Length );

						index++;
					}

					zip.Finish();

					// --

					byte[] c = new byte[buf.Length];
					buf.Seek( 0, SeekOrigin.Begin );
					buf.Read( c, 0, c.Length );

					// --

					zip.Close();

					return c;
				}
			}
		}
Esempio n. 3
0
		/// <summary>
		/// Compress multiple items with the ZIP algorithm.
		/// </summary>
		/// <param name="infos">The items to compress.</param>
		/// <param name="progress1">The progress1.</param>
		/// <param name="progress2">The progress2.</param>
		/// <param name="destinationFilePath">The destination file path.</param>
		private static void CompressHeterogenousToFile(
			CompressHeterogenousInfos infos,
			ICancelableProgress progress1,
			Cancelable progress2,
			FileInfo destinationFilePath )
		{
			if ( destinationFilePath.Exists )
			{
				destinationFilePath.Delete();
			}

			using ( FileStream destinationStream = new FileStream(
				destinationFilePath.FullName,
				FileMode.OpenOrCreate,
				FileAccess.ReadWrite ) )
			{
				Stream realStream =
					CompressHeterogenousToStream(
					infos,
					progress1,
					progress2,
					destinationStream );

				if ( realStream != null )
				{
					realStream.Dispose();
				}
			}
		}
Esempio n. 4
0
		/// <summary>
		/// Compress multiple items with the ZIP algorithm.
		/// </summary>
		/// <param name="infos">The items to compress.</param>
		/// <param name="progress">The progress.</param>
		/// <param name="destinationFilePath">The destination file path.</param>
		public static void CompressHeterogenousToFile(
			CompressHeterogenousInfos infos,
			Cancelable progress,
			FileInfo destinationFilePath )
		{
			CompressHeterogenousToFile(
				infos,
				null,
				progress,
				destinationFilePath );
		}
Esempio n. 5
0
		/// <summary>
		/// Compress multiple items with the ZIP algorithm.
		/// </summary>
		/// <param name="infos">The items to compress.</param>
		/// <param name="progress1">The progress1.</param>
		/// <param name="progress2">The progress2.</param>
		/// <returns>Returns the compressed items.</returns>
		private static byte[] CompressHeterogenous(
			CompressHeterogenousInfos infos,
			ICancelableProgress progress1,
			Cancelable progress2 )
		{
			using ( MemoryStream destinationStream = new MemoryStream() )
			{
				Stream realStream = CompressHeterogenousToStream(
					infos,
					progress1,
					progress2,
					destinationStream );

				if ( realStream == null )
				{
					return null;
				}
				else
				{
					using ( realStream )
					{
						byte[] c = new byte[destinationStream.Length];

						destinationStream.Seek( 0, SeekOrigin.Begin );
						destinationStream.Read( c, 0, c.Length );

						return c;
					}
				}
			}
		}
Esempio n. 6
0
		/// <summary>
		/// Compress multiple items with the ZIP algorithm.
		/// </summary>
		/// <param name="infos">The items to compress.</param>
		/// <param name="progress">The progress.</param>
		/// <returns>Returns the compressed items.</returns>
		public static byte[] CompressHeterogenous(
			CompressHeterogenousInfos infos,
			Cancelable progress )
		{
			return CompressHeterogenous( infos, null, progress );
		}
Esempio n. 7
0
		/// <summary>
		/// Compress multiple items with the ZIP algorithm.
		/// </summary>
		/// <param name="infos">The items to compress.</param>
		/// <returns>Returns the compressed items.</returns>
		public static byte[] CompressHeterogenous(
			CompressHeterogenousInfos infos )
		{
			return CompressHeterogenous( infos, null, null );
		}