/// <summary> /// Adds a file entry to the package by copying data from a stream. /// </summary> /// <param name="packagePath">The fully qualified name of the file to be added.</param> /// <param name="input">The input stream.</param> /// <param name="size">The number of bytes to copy.</param> /// <returns>The package entry added.</returns> /// <remarks> /// The input stream's position will be advanced past the last /// byte read. /// </remarks> public PackageEntry AddFile(string packagePath, Stream input, int size) { PackageEntry entry; byte[] buffer; int cb, cbRemain; if (packageOut == null) { throw new PackageException(ReadOnly); } CheckPath(packagePath); if (entries[packagePath.ToUpper()] != null) { throw new PackageException(AlreadyExists, packagePath); } entry = new PackageEntry(this, packagePath, false); entry.SetSize(size); AddEntry(entry); entry.Serialize(packageOut); buffer = new byte[CopyBufSize]; cbRemain = entry.Size; while (cbRemain > 0) { cb = cbRemain; if (cb > CopyBufSize) { cb = CopyBufSize; } input.Read(buffer, 0, cb); packageOut.Write(buffer, 0, cb); cbRemain -= cb; } return(entry); }
/// <summary> /// Adds a folder entry to the package. /// </summary> /// <param name="packagePath">The fully wualified name of the folder to be added.</param> /// <returns>The package entry added.</returns> public PackageEntry AddFolder(string packagePath) { PackageEntry entry; if (packageOut == null) { throw new PackageException(ReadOnly); } CheckPath(packagePath); if (entries[packagePath.ToUpper()] != null) { throw new PackageException(AlreadyExists, packagePath); } entry = new PackageEntry(this, packagePath, true); entry.SetSize(0); AddEntry(entry); entry.Serialize(packageOut); return(entry); }
/// <summary> /// Adds a file entry to the package. /// </summary> /// <param name="packagePath">The fully qualified name of the file to be added.</param> /// <param name="buffer">The entry data to be added.</param> /// <returns>The package entry added.</returns> public PackageEntry AddFile(string packagePath, byte[] buffer) { PackageEntry entry; if (packageOut == null) { throw new PackageException(ReadOnly); } CheckPath(packagePath); if (entries[packagePath.ToUpper()] != null) { throw new PackageException(AlreadyExists, packagePath); } entry = new PackageEntry(this, packagePath, false); entry.SetSize(buffer.Length); AddEntry(entry); entry.Serialize(packageOut); packageOut.Write(buffer, 0, buffer.Length); return(entry); }