/// <summary> /// Initializes a new instance of the <see cref="PackageFile"/> class. /// The instance is initialized as uncompressed and depends on the file existing prior to creation of the PackageFile /// </summary> /// <param name="ownerPackage">The owner package.</param> public PackageFile(IPackage ownerPackage, string path) { if (ownerPackage == null) { throw new ArgumentNullException("PackageFiles must be created with an owning Package"); } if (string.IsNullOrWhiteSpace(path)) { throw new ArgumentNullException("Path must be non-null and not whitespace."); } string absolutePath = System.IO.Path.Combine(ownerPackage.Location, path); if (System.IO.File.Exists(absolutePath) == false) { throw new System.IO.FileNotFoundException("File must exist"); } Owner = ownerPackage; ID = Guid.NewGuid().ToString("D"); Path = path; using (var stream = new System.IO.FileStream(absolutePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)) { UncompressedSize = stream.Length; UncompressedHash = Package.ComputeHash(stream); } }
/// <summary> /// Packs the file and prepares it for transport /// </summary> public void PackFile() { string inFilePath = System.IO.Path.Combine(Owner.Location, Path); using (var inFile = System.IO.File.OpenRead(inFilePath)) { using (var outFile = new System.IO.MemoryStream()) { CompressStream(inFile, outFile); outFile.Seek(0, System.IO.SeekOrigin.Begin); CompressedSize = outFile.Length; CompressedHash = Package.ComputeHash(outFile); Data = System.Convert.ToBase64String(outFile.GetBuffer(), 0, (int)outFile.Length); IsCompressed = true; } } }