public UnzipReadStream(UnzipArchive archive, CompressionOption compressionLevel) { Archive = archive; Archive.FileActive = true; CompressionLevel = compressionLevel; length = NativeUnzip.CurrentFileLength(Archive.Handle); }
public static void ZipFiles(string path, IEnumerable<string> files, CompressionOption compressionLevel) { using (FileStream fileStream = new FileStream(path, FileMode.Create)) { clsZipFile.ZipFilesToStream(fileStream, files, compressionLevel); } }
public static void ZipFiles(string path, IEnumerable <string> files, CompressionOption compressionLevel = CompressionOption.Normal) { using (FileStream fileStream = new FileStream(path, FileMode.Create)) { ZipHelper.ZipFilesToStream(fileStream, files, compressionLevel); } }
//------------------------------------------------------------------------- // Protected Methods - Package Overrides //------------------------------------------------------------------------- /// <summary> /// Creates a new PackagePart. /// </summary> /// <remarks> /// When creating a new PackagePart we must: /// a) ensure the part does not exist in package /// b) ensure there is a writable package /// c) create a temp part /// d) update active part reference to the temp part /// /// What if a PackagePart with the same Uri already exists? /// Package.CreatePart checks for this. /// /// Do we need to worry about updating relationships and other parts? /// Relationships are a part and are thus intrinsically handled. /// </remarks> /// <param name="partUri">Uri for the part to create.</param> /// <param name="contentType">Content type string.</param> /// <param name="compressionOption">Compression options.</param> /// <returns>A new PackagePart.</returns> protected override PackagePart CreatePartCore( Uri partUri, string contentType, CompressionOption compressionOption) { // Skipping parameter validation as it is done by CreatePart. EnsureTempPackage(); // the underlying temp package does all the physical work PackagePart result = _tempPackage.Value.CreatePart( partUri, contentType, compressionOption); Uri normalizedPartUri = PackUriHelper.GetNormalizedPartUri(partUri); result = new WriteableOnDemandPackagePart( this, result, TempPackagePartFactory); _activeParts.Add(normalizedPartUri, (WriteableOnDemandPackagePart)result); Trace.SafeWrite( Trace.Packaging, "New part {0}({1})#{2} created.", result.Uri, result.ContentType, result.GetHashCode()); return(result); }
protected override PackagePart CreatePartCore (Uri partUri, string contentType, CompressionOption compressionOption) { FakePackagePart p = new FakePackagePart (this, partUri, contentType, compressionOption); Parts.Add (p.Uri, p); CreatedParts.Add (partUri); return p; }
internal XlsxPackage() { Relationships = new List <Relationship>(); WorkbookRelationships = new List <Relationship>(); XmlFiles = new List <XmlFile>(); CompressionOption = CompressionOption.Normal; }
void LoadParts() { parts = new Dictionary <Uri, ZipPackagePart> (new UriComparer()); try { using (UnzipArchive archive = new UnzipArchive(PackageStream)) { // Load the content type map file XmlDocument doc = new XmlDocument(); using (Stream s = archive.GetStream(ContentUri)) doc.Load(s); XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable); manager.AddNamespace("content", ContentNamespace); // The file names in the zip archive are not prepended with '/' foreach (string file in archive.GetFiles()) { if (file.Equals(ContentUri, StringComparison.Ordinal)) { continue; } XmlNode node; CompressionOption compression = archive.GetCompressionLevel(file); if (file == RelationshipUri.ToString().Substring(1)) { CreatePartCore(RelationshipUri, RelationshipContentType, compression); continue; } string xPath = string.Format("/content:Types/content:Override[@PartName='/{0}']", file); node = doc.SelectSingleNode(xPath, manager); if (node == null) { string ext = Path.GetExtension(file); if (ext.StartsWith(".")) { ext = ext.Substring(1); } xPath = string.Format("/content:Types/content:Default[@Extension='{0}']", ext); node = doc.SelectSingleNode(xPath, manager); } // What do i do if the node is null? This means some has tampered with the // package file manually if (node != null) { CreatePartCore(new Uri("/" + file, UriKind.Relative), node.Attributes["ContentType"].Value, compression); } } } } catch { // The archive is invalid - therefore no parts } }
public void AddFileToAASX(string targetUri, string filePath, CompressionOption compressionOption = CompressionOption.Maximum) { string relativeDestination; if (!targetUri.StartsWith(AASX_FOLDER)) { relativeDestination = AASX_FOLDER + targetUri; } else { relativeDestination = targetUri; } Uri uri = PackUriHelper.CreatePartUri(new Uri(relativeDestination, UriKind.Relative)); ClearRelationshipAndPartFromPackagePart(specPart, SUPPLEMENTAL_RELATIONSHIP_TYPE, uri); string contentType = GetContentType(filePath); PackagePart packagePart = aasxPackage.CreatePart(uri, contentType, compressionOption); specPart.CreateRelationship(packagePart.Uri, TargetMode.Internal, SUPPLEMENTAL_RELATIONSHIP_TYPE); CopyFileToPackagePart(packagePart, filePath); }
XpsDocument( string path, FileAccess packageAccess, CompressionOption compressionOption ) : base(new XpsManager(path, packageAccess, compressionOption ) ) { if (null == path) { throw new ArgumentNullException("path"); } this.Uri = new Uri(path, UriKind.RelativeOrAbsolute); // //The URI has to be absolute //If the path passed in is relative append it to the //current working directory // if (!Uri.IsAbsoluteUri) { Uri = new Uri(new Uri(Directory.GetCurrentDirectory() + "/"), this.Uri); } CurrentXpsManager.XpsDocument = this; Initialize(); }
public Relationship AddImageToTree(ImageHash hash, string extension, string relationshipType, string locationHint, string parentLocation, ContentTypeAction ctypeAction, out bool newBlob) { PhantomPart phantomPart = new PhantomPart(); phantomPart.ContentType = "image/" + extension; if (_blobPathsByUniqueId.TryGetValue(hash, out string value)) { phantomPart.Location = value; newBlob = false; } else { int num; switch (extension) { default: num = 0; break; case "jpg": case "png": case "gif": num = -1; break; } CompressionOption compressionOption = (CompressionOption)num; value = string.Format(CultureInfo.InvariantCulture, UniqueLocation(locationHint), extension); _blobPathsByUniqueId[hash] = value; phantomPart.Location = value; _parts.Add(phantomPart.Location, phantomPart); _package.CreatePart(new Uri(WordOpenXmlUtils.CleanName(phantomPart.Location), UriKind.Relative), phantomPart.ContentType, compressionOption); newBlob = true; } return(AddRelationship(phantomPart.Location, relationshipType, parentLocation)); }
/// <summary> /// This method is for custom implementation for the underlying file format /// Adds a new item to the zip archive corresponding to the PackagePart in the package. /// </summary> /// <param name="partUri">PartName</param> /// <param name="contentType">Content type of the part</param> /// <param name="compressionOption">Compression option for this part</param> /// <returns></returns> /// <exception cref="ArgumentNullException">If partUri parameter is null</exception> /// <exception cref="ArgumentNullException">If contentType parameter is null</exception> /// <exception cref="ArgumentException">If partUri parameter does not conform to the valid partUri syntax</exception> /// <exception cref="ArgumentOutOfRangeException">If CompressionOption enumeration [compressionOption] does not have one of the valid values</exception> protected override PackagePart CreatePartCore(Uri partUri, string contentType, CompressionOption compressionOption) { //Validating the PartUri - this method will do the argument checking required for uri. partUri = PackUriHelper.ValidatePartUri(partUri); if (contentType == null) { throw new ArgumentNullException(nameof(contentType)); } Package.ThrowIfCompressionOptionInvalid(compressionOption); // Convert Metro CompressionOption to Zip CompressionMethodEnum. CompressionLevel level; GetZipCompressionMethodFromOpcCompressionOption(compressionOption, out level); // Create new Zip item. // We need to remove the leading "/" character at the beginning of the part name. // The partUri object must be a ValidatedPartUri string zipItemName = ((PackUriHelper.ValidatedPartUri)partUri).PartUriString.Substring(1); ZipArchiveEntry zipArchiveEntry = _zipArchive.CreateEntry(zipItemName, level); //Store the content type of this part in the content types stream. _contentTypeHelper.AddContentType((PackUriHelper.ValidatedPartUri)partUri, new ContentType(contentType), level); return(new ZipPackagePart(this, zipArchiveEntry.Archive, zipArchiveEntry, _zipStreamManager, (PackUriHelper.ValidatedPartUri)partUri, contentType, compressionOption)); }
XpsDocument( Package package, CompressionOption compressionOption ) : this(package, compressionOption, null) { }
public ArchiveCryptManager() { path = PathOption.GetInstance(); encryptor = new EncryptionOption(); compressor = new CompressionOption(); compressAndEncrypt = new CompressAndEncryptOption(); }
// convert from Zip CompressionMethodEnum and DeflateOptionEnum to Metro CompressionOption private static CompressionOption GetCompressionOptionFromZipFileInfo(ZipArchiveEntry zipFileInfo) { // Note: we can't determine compression method / level from the ZipArchiveEntry. CompressionOption result = CompressionOption.Normal; return(result); }
/// <summary> /// Executes the task. /// </summary> /// <returns></returns> public override bool Execute() { base.Log.LogMessage(Resources.AddFilesToZipPackage_AddFilesToArchive, this.SourceArchive, this.SourcePath, this.MatchExpression); // Ensure source path exists if (!Directory.Exists(this.SourcePath)) { base.Log.LogError(Resources.AddFileToZipPackage_SourcePathNotExist, this.SourcePath); return(false); } // Filter files var matchExpression = !string.IsNullOrEmpty(this.MatchExpression) ? this.MatchExpression : "*"; // Ensure we have a valid CompressionLevel if (!string.IsNullOrWhiteSpace(this.CompressionLevel)) { if (!Enum.TryParse <CompressionOption>(this.CompressionLevel, true, out this.packageCompressionLevel) || (this.packageCompressionLevel != CompressionOption.Normal && this.packageCompressionLevel != CompressionOption.Maximum && this.packageCompressionLevel != CompressionOption.NotCompressed)) { base.Log.LogWarning(Resources.AddFileToZipPackage_CompressionLevelNotSupported, new object[] { this.CompressionLevel, string.Join(",", new[] { CompressionOption.Normal.ToString(), CompressionOption.Maximum.ToString(), CompressionOption.NotCompressed.ToString() }) }); this.packageCompressionLevel = CompressionOption.NotCompressed; } } AddFilesToArchive(this.SourceArchive, matchExpression, this.packageCompressionLevel); return(!base.Log.HasLoggedErrors); }
public virtual void WriteDocument(Stream stream, TagCompound tag, CompressionOption compression) { bool createWriter; if (compression == CompressionOption.On) { throw new NotSupportedException("Compression is not supported."); } createWriter = _writer == null; if (createWriter) { XmlWriterSettings settings; settings = new XmlWriterSettings { Indent = true, Encoding = Encoding.UTF8 }; _writer = XmlWriter.Create(stream, settings); _writer.WriteStartDocument(true); } this.WriteTag(tag, WriteTagOptions.None); if (createWriter) { _writer.WriteEndDocument(); _writer.Flush(); _writer = null; } }
/// <summary> /// Protected constructor for the abstract Base class. /// This is the current contract between the subclass and the base class /// If we decide some registration mechanism then this might change /// /// NOTE : If you are using this constructor from your subclass or passing a null /// for the content type parameter, be sure to implement the GetContentTypeCore /// method, as that will be called to get the content type value. This is provided /// to enable lazy initialization of the ContentType property. /// /// </summary> /// <param name="package">Package in which this part is being created</param> /// <param name="partUri">uri of the part</param> /// <param name="contentType">Content Type of the part, can be null if the value /// is unknown at the time of construction. However the value has to be made /// available anytime the ContentType property is called. A null value only indicates /// that the value will be provided later. Every PackagePart must have a valid /// Content Type</param> /// <param name="compressionOption">compression option for this part</param> /// <exception cref="ArgumentNullException">If parameter "package" is null</exception> /// <exception cref="ArgumentNullException">If parameter "partUri" is null</exception> /// <exception cref="ArgumentOutOfRangeException">If CompressionOption enumeration [compressionOption] does not have one of the valid values</exception> /// <exception cref="ArgumentException">If parameter "partUri" does not conform to the valid partUri syntax</exception> protected PackagePart(Package package, Uri partUri, string contentType, CompressionOption compressionOption) { if (package == null) { throw new ArgumentNullException("package"); } if (partUri == null) { throw new ArgumentNullException("partUri"); } Package.ThrowIfCompressionOptionInvalid(compressionOption); _uri = PackUriHelper.ValidatePartUri(partUri); _container = package; if (contentType == null) { _contentType = null; } else { _contentType = new ContentType(contentType); } _requestedStreams = null; _compressionOption = compressionOption; _isRelationshipPart = PackUriHelper.IsRelationshipPartUri(partUri); }
public UnzipReadStream (UnzipArchive archive, CompressionOption compressionLevel) { Archive = archive; Archive.FileActive = true; CompressionLevel = compressionLevel; length = NativeVersion.Use32Bit ? NativeUnzip.CurrentFileLength32 (Archive.Handle) : NativeUnzip.CurrentFileLength64 (Archive.Handle); }
/// <summary> /// Initializes a new instance of the <see cref="ByteArraySerializer"/> class. /// </summary> /// <param name="compressionOption">The compression option.</param> public ByteArraySerializer(CompressionOption compressionOption) { _serializer = new WireSerializer(); _memoryManager = new RecyclableMemoryStreamManager(); _compressionOption = compressionOption; _streamCompressor = GetCompressor(compressionOption); }
public XlsxWriterInternal(Workbook workbook, CompressionOption compressionOption) { _workbook = workbook; _relationshipCounter = new RelationshipCounter(); _sharedStrings = new SharedStrings(); _package = new XlsxPackage { CompressionOption = compressionOption }; _styles = GetXlsxStyles(); }
/// <summary> /// Constructs a ZipPackagePart. This is called from ZipPackage.CreatePartCore in streaming /// production. /// No piece is created until the first write operation on the associated stream. Therefore /// this constructor does not take a ZipFileInfo. /// </summary> /// <param name="container"></param> /// <param name="zipArchive"></param> /// <param name="partUri"></param> /// <param name="compressionOption"></param> /// <param name="contentType"></param> internal ZipPackagePart(ZipPackage container, ZipArchive zipArchive, PackUriHelper.ValidatedPartUri partUri, string contentType, CompressionOption compressionOption) : base(container, partUri, contentType, compressionOption) { _zipArchive = zipArchive; }
public static void AddFile(Package result,string baseFolder,string relativePath,CompressionOption compressionOption) { FileInfo f = new FileInfo(baseFolder+relativePath.Replace("/","\\")); //f.Extension; PackagePart data = result.CreatePart(new Uri(relativePath, UriKind.Relative), GetContentType(f.Name), compressionOption); StreamReader reader = new StreamReader(baseFolder+relativePath.Replace("/","\\")); WriteAll(data.GetStream(), reader.BaseStream); reader.Close(); }
/// <summary> /// Creates a new PackagePart. /// </summary> /// <remarks> /// When creating a new PackagePart we must: /// a) ensure the part does not exist in package /// b) ensure there is a writable package /// c) create a temp part /// d) update active part reference to the temp part /// /// What if a PackagePart with the same Uri already exists? /// Package.CreatePart checks for this. /// /// Do we need to worry about updating relationships and other parts? /// Relationships are a part and are thus intrinsically handled. /// </remarks> /// <param name="partUri">Uri for the part to create.</param> /// <param name="contentType">Content type string.</param> /// <param name="compressionOption">Compression options.</param> /// <returns>A new PackagePart.</returns> protected override PackagePart CreatePartCore( Uri partUri, string contentType, CompressionOption compressionOption) { // Ensure that modifying this contentType is permitted. if (!IsValidContentType(contentType)) { throw new ArgumentException(SR.Get(SRID.PackagePartTypeNotWritable), "contentType"); } return(base.CreatePartCore(partUri, contentType, compressionOption)); }
public StreamInfo CreateStream( string name, CompressionOption compressionOption, EncryptionOption encryptionOption) { return((StreamInfo)_storageRootType.InvokeMember("CreateStream", InstanceMethodBindFlags, null, _storageRoot, new object[3] { name, compressionOption, encryptionOption })); }
protected internal PackagePart(Package package, Uri partUri, string contentType, CompressionOption compressionOption) { Check.Package(package); Check.PartUri(partUri); Check.ContentTypeIsValid(contentType); Package = package; Uri = partUri; ContentType = contentType; CompressionOption = compressionOption; RelationshipsPartUri = PackUriHelper.GetRelationshipPartUri(Uri); }
public static byte[] ZipFilesToByteArray(IEnumerable <string> files, CompressionOption compressionLevel = CompressionOption.Normal) { byte[] zipBytes = default(byte[]); using (MemoryStream memoryStream = new MemoryStream()) { ZipHelper.ZipFilesToStream(memoryStream, files, compressionLevel); memoryStream.Flush(); zipBytes = memoryStream.ToArray(); } return(zipBytes); }
protected internal PackagePart (Package package, Uri partUri, string contentType, CompressionOption compressionOption) { Check.Package (package); Check.PartUri (partUri); Check.ContentTypeIsValid (contentType); Package = package; Uri = partUri; ContentType = contentType; CompressionOption = compressionOption; RelationshipsPartUri = PackUriHelper.GetRelationshipPartUri(Uri); }
public static void CreateZipFromFolder(string result,string folder,CompressionOption compressionOption) { if (File.Exists(result)) File.Delete(result); var zip = Package.Open(result,FileMode.CreateNew); DirectoryInfo dir = new DirectoryInfo(folder); foreach (var el in GetDirFiles(folder)) { string filename = el; AddFile(zip, dir.FullName,@"/"+ filename.Replace("\\","/"),compressionOption); } zip.Flush(); zip.Close(); }
public CompressorUnitTest() { var compressionOptions = new CompressionOption() { TriggerByteSize = 100 * 1024 }; _compressor = new Compressor(compressionOptions); var dataPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory !); _personJsonString = File.ReadAllText(Path.Combine(dataPath, PersonJsonFileName)); _personBase64GzippedString = $"{compressionOptions.Base64Prefix}{File.ReadAllText(Path.Combine(dataPath, PersonJsonGzipBase64FileName))}"; }
/// <summary> /// Constructs a ZipPackagePart for an atomic (i.e. non-interleaved) part. /// This is called from the ZipPackage class as a result of GetPartCore, /// GetPartsCore or CreatePartCore methods /// </summary> /// <param name="zipPackage"></param> /// <param name="zipArchive"></param> /// <param name="zipArchiveEntry"></param> /// <param name="zipStreamManager"></param> /// <param name="partUri"></param> /// <param name="compressionOption"></param> /// <param name="contentType"></param> internal ZipPackagePart(ZipPackage zipPackage, ZipArchive zipArchive, ZipArchiveEntry zipArchiveEntry, ZipStreamManager zipStreamManager, PackUriHelper.ValidatedPartUri partUri, string contentType, CompressionOption compressionOption) : base(zipPackage, partUri, contentType, compressionOption) { _zipPackage = zipPackage; _zipArchive = zipArchive; _zipStreamManager = zipStreamManager; _zipArchiveEntry = zipArchiveEntry; }
//------------------------------------------------------ // // Public Events // //------------------------------------------------------ // None //------------------------------------------------------ // // Internal Constructors // //------------------------------------------------------ #region Internal Constructors /// <summary> /// Constructs a ZipPackagePart for an atomic (i.e. non-interleaved) part. /// This is called from the ZipPackage class as a result of GetPartCore, /// GetPartsCore or CreatePartCore methods /// </summary> /// <param name="zipPackage"></param> /// <param name="zipArchive"></param> /// <param name="zipArchiveEntry"></param> /// <param name="zipStreamManager"></param> /// <param name="partUri"></param> /// <param name="compressionOption"></param> /// <param name="contentType"></param> internal ZipPackagePart(ZipPackage zipPackage, ZipArchive zipArchive, ZipArchiveEntry zipArchiveEntry, ZipStreamManager zipStreamManager, PackUriHelper.ValidatedPartUri partUri, string contentType, CompressionOption compressionOption) : base(zipPackage, partUri, contentType, compressionOption) { _zipPackage = zipPackage; _zipArchive = zipArchive; _zipStreamManager = zipStreamManager; _zipArchiveEntry = zipArchiveEntry; }
public static bool CompressSingleStreamToFile(Stream stream, string fileUri, string filePath, CompressionOption option = CompressionOption.Normal) { using (var pack = Package.Open(filePath, FileMode.Create)) { var part = pack.CreatePart(new Uri(fileUri, UriKind.Relative), System.Net.Mime.MediaTypeNames.Text.Xml, option); if (part != null) { CopyStream(stream, part.GetStream()); } } return(true); }
/// <summary> /// Creates a new instance relative to the given parent /// </summary> /// <param name="parent">The parent storage</param> /// <param name="streamName">Path to stream under parent storage</param> /// <param name="compressionOption">CompressionOption</param> /// <param name="encryptionOption">EncryptionOption</param> internal StreamInfo(StorageInfo parent, string streamName, CompressionOption compressionOption, EncryptionOption encryptionOption) { // Parameter validation CU.CheckAgainstNull(parent, "parent"); CU.CheckStringAgainstNullAndEmpty(streamName, "streamName"); // Parse path relative to given parent. BuildStreamInfoRelativeToStorage(parent, streamName); _compressionOption = compressionOption; _encryptionOption = encryptionOption; _streamReference = new CompoundFileStreamReference(this.parentStorage.FullNameInternal, this.core.streamName); }
public Format Save(string filename, Format format, string[] src, CompressionOption options) { var dest = Get(filename); using (var obj = new ArchiveWriter(format, options ?? new())) { foreach (var e in src) { obj.Add(GetSource(e)); } obj.Save(dest); obj.Clear(); } using (var obj = Io.Open(dest)) return(Formatter.FromStream(obj)); }
/// <summary> /// Ensures that the PackageRelationship PackagePart has been created - lazy init /// </summary> /// <remarks> /// </remarks> private void EnsureRelationshipPart() { if (_relationshipPart == null || _relationshipPart.IsDeleted) { if (_package.PartExists(_uri)) { _relationshipPart = _package.GetPart(_uri); ThrowIfIncorrectContentType(_relationshipPart.ValidatedContentType); } else { CompressionOption compressionOption = _sourcePart == null ? CompressionOption.NotCompressed : _sourcePart.CompressionOption; _relationshipPart = _package.CreatePart(_uri, PackagingUtilities.RelationshipPartContentType.ToString(), compressionOption); } } }
public PackagePart CreatePart(Uri partUri, string contentType, CompressionOption compressionOption) { CheckIsReadOnly(); Check.PartUri(partUri); Check.ContentTypeIsValid(contentType); if (PartExists(partUri)) { throw new InvalidOperationException("This partUri is already contained in the package"); } PackagePart part = CreatePartCore(partUri, contentType, compressionOption); PartsCollection.Parts.Add(part); return(part); }
XpsDocument( Package package, CompressionOption compressionOption, String path ) : base(new XpsManager(package, compressionOption ) ) { if (path != null) { this.Uri = new Uri(path, UriKind.RelativeOrAbsolute); } CurrentXpsManager.XpsDocument = this; Initialize(); }
public static void OpenFile(UnzipHandle handle, string name, out CompressionOption level) { if (unzLocateFile(handle, name, (int)ZipStringComparison.CaseInsensitive) != 0) { throw new Exception("The file doesn't exist"); } int method, compression; // '0' means do not open in raw mode (raw == do not decompress) if (unzOpenCurrentFile2(handle, out method, out compression, 0) != 0) { throw new Exception("The file could not be opened"); } level = ConvertCompression(method == 0 ? 0 : compression); }
public CompressionBenchmark() { //docker run --name some-redis -d -p 6379:6379 redis redis-server --appendonly yes var configurationOptions = ConfigurationOptions.Parse(_redisConnectionString); configurationOptions.ReconnectRetryPolicy = new ExponentialRetry(1000); var compressionOptions = new CompressionOption() { TriggerByteSize = 100 * 1024 }; var options = new RedisCacheOptions() { ConfigurationOptions = configurationOptions, CompressionOption = compressionOptions }; var mockWithCompression = new Mock <IOptionsMonitor <RedisCacheOptions> >(); mockWithCompression.Setup(m => m.Get(nameof(CompressionBenchmark))).Returns(options); _redisServiceWithCompression = new RedisService <CompressionBenchmark>(mockWithCompression.Object); var configurationOptions2 = ConfigurationOptions.Parse(_redisConnectionString); configurationOptions2.ReconnectRetryPolicy = new ExponentialRetry(1000); var options2 = new RedisCacheOptions() { ConfigurationOptions = configurationOptions2, }; var mockWithoutCompression = new Mock <IOptionsMonitor <RedisCacheOptions> >(); mockWithoutCompression.Setup(m => m.Get(nameof(CompressionBenchmark))).Returns(options2); _redisServiceWithoutCompression = new RedisService <CompressionBenchmark>(mockWithoutCompression.Object); var dataPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory !); //prepare the person's json data string var jsonString = File.ReadAllText(Path.Combine(dataPath, PersonJsonFileName)); var persons = JsonConvert.DeserializeObject <List <Person> >(jsonString); _personJsonString = JsonConvert.SerializeObject(persons); //prepare the gzipped Base64 data _gzippedBase64String = $"{compressionOptions.Base64Prefix}{File.ReadAllText(Path.Combine(dataPath, PersonJsonGzipBase64FileName))}"; }
private static void ZipFilesToStream(FileStream destination, IEnumerable<string> files, CompressionOption compressionLevel) { using (Package package = Package.Open(destination, FileMode.Create)) { foreach (string path in files) { Uri fileUri = new Uri(@"/" + Path.GetFileName(path), UriKind.Relative); string contentType = @"data/" + clsZipFile.GetFileExtentionName(path); using (Stream zipStream = package.CreatePart(fileUri, contentType, compressionLevel).GetStream()) { CopyToStream(destination, zipStream); } } } }
/// <summary> /// Protected constructor for the abstract Base class. /// This is the current contract between the subclass and the base class /// If we decide some registration mechanism then this might change /// /// NOTE : If you are using this constructor from your subclass or passing a null /// for the content type parameter, be sure to implement the GetContentTypeCore /// method, as that will be called to get the content type value. This is provided /// to enable lazy initialization of the ContentType property. /// /// </summary> /// <param name="package">Package in which this part is being created</param> /// <param name="partUri">uri of the part</param> /// <param name="contentType">Content Type of the part, can be null if the value /// is unknown at the time of construction. However the value has to be made /// available anytime the ContentType property is called. A null value only indicates /// that the value will be provided later. Every PackagePart must have a valid /// Content Type</param> /// <param name="compressionOption">compression option for this part</param> /// <exception cref="ArgumentNullException">If parameter "package" is null</exception> /// <exception cref="ArgumentNullException">If parameter "partUri" is null</exception> /// <exception cref="ArgumentOutOfRangeException">If CompressionOption enumeration [compressionOption] does not have one of the valid values</exception> /// <exception cref="ArgumentException">If parameter "partUri" does not conform to the valid partUri syntax</exception> protected PackagePart(Package package, Uri partUri, string contentType, CompressionOption compressionOption) { if (package == null) throw new ArgumentNullException("package"); if (partUri == null) throw new ArgumentNullException("partUri"); Package.ThrowIfCompressionOptionInvalid(compressionOption); _uri = PackUriHelper.ValidatePartUri(partUri); _container = package; if (contentType == null) _contentType = null; else _contentType = new ContentType(contentType); _requestedStreams = null; _compressionOption = compressionOption; _isRelationshipPart = PackUriHelper.IsRelationshipPartUri(partUri); }
public virtual void WriteDocument(Stream stream, TagCompound tag, CompressionOption compression) { if (compression != CompressionOption.Off) { using (Stream compressedStream = new GZipStream(stream, CompressionMode.Compress, true)) { _stream = compressedStream; this.WriteTag(tag, WriteTagOptions.None); } } else { _stream = stream; this.WriteTag(tag, WriteTagOptions.None); } }
internal static void ThrowIfCompressionOptionInvalid(CompressionOption compressionOption) { //We do the enum check as suggested by the following condition for performance reasons. if (compressionOption < CompressionOption.NotCompressed || compressionOption > CompressionOption.SuperFast) throw new ArgumentOutOfRangeException(nameof(compressionOption)); }
/// <summary> /// Creates a new part in the package. An empty stream corresponding to this part will be created in the /// package. If a part with the specified uri already exists then we throw an exception. /// This methods will call the CreatePartCore method which will create the actual PackagePart in the package. /// </summary> /// <param name="partUri">Uri of the PackagePart that is to be added</param> /// <param name="contentType">ContentType of the stream to be added</param> /// <param name="compressionOption">CompressionOption describing compression configuration /// for the new part. This compression apply only to the part, it doesn't affect relationship parts or related parts. /// This parameter is optional. </param> /// <returns></returns> /// <exception cref="ObjectDisposedException">If this Package object has been disposed</exception> /// <exception cref="IOException">If the package is readonly, it cannot be modified</exception> /// <exception cref="ArgumentNullException">If partUri parameter is null</exception> /// <exception cref="ArgumentNullException">If contentType parameter is null</exception> /// <exception cref="ArgumentException">If partUri parameter does not conform to the valid partUri syntax</exception> /// <exception cref="ArgumentOutOfRangeException">If CompressionOption enumeration [compressionOption] does not have one of the valid values</exception> /// <exception cref="InvalidOperationException">If a PackagePart with the given partUri already exists in the Package</exception> public PackagePart CreatePart(Uri partUri, string contentType, CompressionOption compressionOption) { ThrowIfObjectDisposed(); ThrowIfReadOnly(); if (partUri == null) throw new ArgumentNullException(nameof(partUri)); if (contentType == null) throw new ArgumentNullException(nameof(contentType)); ThrowIfCompressionOptionInvalid(compressionOption); PackUriHelper.ValidatedPartUri validatedPartUri = PackUriHelper.ValidatePartUri(partUri); if (_partList.ContainsKey(validatedPartUri)) throw new InvalidOperationException(SR.PartAlreadyExists); // Add the part to the _partList if there is no prefix collision // Note: This is the only place where we pass a null to this method for the part and if the // methods returns successfully then we replace the null with an actual part. AddIfNoPrefixCollisionDetected(validatedPartUri, null /* since we don't have a part yet */); PackagePart addedPart = CreatePartCore(validatedPartUri, contentType, compressionOption); //Set the entry for this Uri with the actual part _partList[validatedPartUri] = addedPart; return addedPart; }
/// <summary> /// Creates a new instance relative to the given parent /// </summary> /// <param name="parent">The parent storage</param> /// <param name="streamName">Path to stream under parent storage</param> /// <param name="compressionOption">CompressionOption</param> /// <param name="encryptionOption">EncryptionOption</param> internal StreamInfo( StorageInfo parent, string streamName, CompressionOption compressionOption, EncryptionOption encryptionOption ) { // Parameter validation CU.CheckAgainstNull( parent, "parent" ); CU.CheckStringAgainstNullAndEmpty( streamName, "streamName" ); // Parse path relative to given parent. BuildStreamInfoRelativeToStorage( parent, streamName); _compressionOption = compressionOption; _encryptionOption = encryptionOption; _streamReference = new CompoundFileStreamReference(this.parentStorage.FullNameInternal, this.core.streamName); }
// Inspect the transforms applied this stream and retreive the compression and // RM encryption options private void EnsureTransformInformation() { if (_needToGetTransformInfo && InternalExists()) { _encryptionOption = EncryptionOption.None; _compressionOption = CompressionOption.NotCompressed; //If the StreamInfo exists we go on to check if correct transform has been //applied to the Stream DataSpaceManager dsm = parentStorage.Root.GetDataSpaceManager(); List<IDataTransform> transforms = dsm.GetTransformsForStreamInfo(this); foreach (IDataTransform dataTransform in transforms) { string id = dataTransform.TransformIdentifier as string; if (id != null) { id = id.ToUpperInvariant(); if (String.CompareOrdinal(id, RightsManagementEncryptionTransform.ClassTransformIdentifier.ToUpperInvariant()) == 0 && (dataTransform as RightsManagementEncryptionTransform) != null) { _encryptionOption = EncryptionOption.RightsManagement; } else if (String.CompareOrdinal(id, CompressionTransform.ClassTransformIdentifier.ToUpperInvariant()) == 0 && (dataTransform as CompressionTransform) != null) { // We don't persist the compression level used during compression process // When we access the stream, all we can determine is whether it is compressed or not // In all our scenarios, the level we use is Level 9 which is equivalent to Maximum _compressionOption = CompressionOption.Maximum; } } } _needToGetTransformInfo = false; } }
public StreamInfo CreateStream (string name, CompressionOption compressionOption, EncryptionOption encryptionOption) { throw new NotImplementedException (); }
protected override PackagePart CreatePartCore(Uri parentUri, string contentType, CompressionOption compressionOption) { throw new NotImplementedException (); }
/***********************************************************************/ // public Methods /// <summary> /// Creates "this" stream /// </summary> /// <param name="name">Name of stream</param> /// <param name="compressionOption">CompressionOptiont</param> /// <param name="encryptionOption">EncryptionOption</param> /// <returns>Reference to new stream</returns> public StreamInfo CreateStream( string name, CompressionOption compressionOption, EncryptionOption encryptionOption ) { CheckDisposedStatus(); //check the arguments if( null == name ) throw new ArgumentNullException("name"); // Stream names: we preserve casing, but do case-insensitive comparison (Native CompoundFile API behavior) if (((IEqualityComparer) CU.StringCaseInsensitiveComparer).Equals(name, EncryptedPackageEnvelope.PackageStreamName)) throw new ArgumentException(SR.Get(SRID.StreamNameNotValid,name)); //create a new streaminfo object StreamInfo streamInfo = new StreamInfo(this, name, compressionOption, encryptionOption); if (streamInfo.InternalExists()) { throw new IOException(SR.Get(SRID.StreamAlreadyExist)); } //Define the compression and encryption options in the dataspacemanager DataSpaceManager manager = Root.GetDataSpaceManager(); string dataSpaceLabel = null; if (manager != null) { //case : Compression option is set. Stream need to be compressed. Define compression transform. //At this time, we only treat CompressionOption - Normal and None. The rest are treated as Normal if (compressionOption != CompressionOption.NotCompressed) { //If it is not defined already, define it. if (!manager.TransformLabelIsDefined(sc_compressionTransformName)) manager.DefineTransform(CompressionTransform.ClassTransformIdentifier, sc_compressionTransformName); } //case : Encryption option is set. Stream need to be encrypted. Define encryption transform. if (encryptionOption == EncryptionOption.RightsManagement) { //If it not defined already, define it. if (!manager.TransformLabelIsDefined(EncryptedPackageEnvelope.EncryptionTransformName)) { //We really cannot define RM transform completely here because the transform initialization cannot be done here without publishlicense and cryptoprovider. //However it will always be defined because this method is accessed only through an EncryptedPackageEnvelope and RM transform is always defined in EncryptedPackageEnvelope.Create() throw new SystemException(SR.Get(SRID.RightsManagementEncryptionTransformNotFound)); } } //Now find the dataspace label that we need to define these transforms in. //CASE: When both CompressionOption and EncryptionOption are set if ( (compressionOption != CompressionOption.NotCompressed) && (encryptionOption == EncryptionOption.RightsManagement) ) { dataSpaceLabel = sc_dataspaceLabelRMEncryptionNormalCompression; if (!manager.DataSpaceIsDefined(dataSpaceLabel)) { string[] transformStack = new string[2]; //compress the data first. then encrypt it. This ordering will cause the content to be compressed, then encrypted, then written to the stream. transformStack[0] = EncryptedPackageEnvelope.EncryptionTransformName; transformStack[1] = sc_compressionTransformName; manager.DefineDataSpace(transformStack, dataSpaceLabel); } } //CASE : when CompressionOption alone is set else if ( (compressionOption != CompressionOption.NotCompressed) && (encryptionOption == EncryptionOption.None) ) { dataSpaceLabel = sc_dataspaceLabelNoEncryptionNormalCompression; if (!manager.DataSpaceIsDefined(dataSpaceLabel)) { string[] transformStack = new string[1]; transformStack[0] = sc_compressionTransformName; manager.DefineDataSpace(transformStack, dataSpaceLabel); } } //CASE : when EncryptionOption alone is set else if (encryptionOption == EncryptionOption.RightsManagement) { dataSpaceLabel = EncryptedPackageEnvelope.DataspaceLabelRMEncryptionNoCompression; if (!manager.DataSpaceIsDefined(dataSpaceLabel)) { string[] transformStack = new string[1]; transformStack[0] = EncryptedPackageEnvelope.EncryptionTransformName; manager.DefineDataSpace(transformStack, dataSpaceLabel); } } //All the other cases are not handled at this point. } //create the underlying stream if (null == dataSpaceLabel) streamInfo.Create(); //create the stream with default parameters else streamInfo.Create(dataSpaceLabel); //create the stream in the defined dataspace return streamInfo; }
/// <summary> /// Initializes the <see cref="LightningStorageUtils"/> class. /// </summary> static LightningStorageUtils() { _compressionOption = (CompressionOption) Enum.Parse(typeof(CompressionOption), (ConfigurationManager.AppSettings["StorageEngine.Compression"] ?? "LZ4"), true); _serializer = new ByteArraySerializer(_compressionOption); }
public static void Create(string packagePath, TemplatePackageManifest manifest, IEnumerable<string> filePaths, string rsaParamsXml, CompressionOption compression) { Create(packagePath, manifest, filePaths, rsaParamsXml, compression, true); }
/// <summary> /// Create a package in one step. /// </summary> /// <param name="packagePath">The full file path were the package should be written to. Any existing file will be overwritten.</param> /// <param name="manifest">The manifest of the package. Passing null creates a package without a manifest.</param> /// <param name="filePaths">A complete list of all file paths to be added to the package.</param> /// <param name="rsaParamsXml">RSA key serialized to XML. It can be a public/private key pair or only a public key. If null, save an unencrypted package.</param> /// <param name="compression">The compression level.</param> public static void Create(string packagePath, TemplatePackageManifest manifest, IEnumerable<string> filePaths, string rsaParamsXml, CompressionOption compression, bool checkForGeneratedFiles) { TemplatePackage package = new TemplatePackage(); package.CheckForGeneratedFiles = checkForGeneratedFiles; // package.CurrentCompression = compression; foreach (var s in filePaths) { package.AddFile(s); } package.Manifest = manifest; package.SaveAs(packagePath, rsaParamsXml); }
//------------------------------------------------------ // // Uninteresting (but required) overrides // //------------------------------------------------------ #region Uninteresting (but required) overrides protected override PackagePart CreatePartCore(Uri uri, string contentType, CompressionOption compressionOption) { return null; }
public static void OpenFile (UnzipHandle handle, string name, out CompressionOption level) { if (unzLocateFile (handle, name, (int) ZipStringComparison.CaseInsensitive) != 0) throw new Exception ("The file doesn't exist"); int method, compression; // '0' means do not open in raw mode (raw == do not decompress) if (unzOpenCurrentFile2 (handle, out method, out compression, 0) != 0) throw new Exception ("The file could not be opened"); level = ConvertCompression (method == 0 ? 0 : compression); }
/// <summary> /// Constructs a ZipPackagePart for an interleaved part. This is called outside of streaming /// production when an interleaved part is encountered in the package. /// </summary> /// <param name="container"></param> /// <param name="zipArchive"></param> /// <param name="pieces"></param> /// <param name="partUri"></param> /// <param name="compressionOption"></param> /// <param name="contentType"></param> internal ZipPackagePart(ZipPackage container, ZipArchive zipArchive, List<PieceInfo> pieces, PackUriHelper.ValidatedPartUri partUri, string contentType, CompressionOption compressionOption) :base(container, partUri, contentType, compressionOption) { _zipArchive = zipArchive; _pieces = pieces; }
/// <summary> /// This method is for custom implementation corresponding to the underlying file format. /// This method will actually add a new part to the package. An empty part should be /// created as a result of this call. /// </summary> /// <param name="partUri"></param> /// <param name="contentType"></param> /// <param name="compressionOption"></param> /// <returns></returns> protected abstract PackagePart CreatePartCore(Uri partUri, string contentType, CompressionOption compressionOption);
//------------------------------------------------------ // // Public Events // //------------------------------------------------------ // None //------------------------------------------------------ // // Internal Constructors // //------------------------------------------------------ #region Internal Constructors /// <summary> /// Constructs a ZipPackagePart for an atomic (i.e. non-interleaved) part. /// This is called from the ZipPackage class as a result of GetPartCore, /// GetPartsCore or CreatePartCore methods /// </summary> /// <param name="container"></param> /// <param name="zipFileInfo"></param> /// <param name="partUri"></param> /// <param name="compressionOption"></param> /// <param name="contentType"></param> internal ZipPackagePart(ZipPackage container, ZipFileInfo zipFileInfo, PackUriHelper.ValidatedPartUri partUri, string contentType, CompressionOption compressionOption) :base(container, partUri, contentType, compressionOption) { _zipArchive = zipFileInfo.ZipArchive; _zipFileInfo = zipFileInfo; }
public FakePackagePart (Package package, Uri partUri, string contentType, CompressionOption compressionOption) : base (package, partUri, contentType, compressionOption) { Init (); }
public PackagePart CreatePart (Uri partUri, string contentType, CompressionOption compressionOption) { CheckIsReadOnly (); Check.PartUri (partUri); Check.ContentTypeIsValid (contentType); if (PartExists (partUri)) throw new InvalidOperationException ("This partUri is already contained in the package"); PackagePart part = CreatePartCore (partUri, contentType, compressionOption); PartsCollection.Parts.Add (part); return part; }