• Lexical
Show / Hide Table of Contents
  • Lexical.FileSystem
    • Introduction
    • Abstractions
      • IFileSystem
        • IFileSystem
        • IFileSystemBrowse
        • IFileSystemCreateDirectory
        • IFileSystemDelete
        • IFileSystemFileAttribute
        • IFileSystemMount
        • IFileSystemMove
        • IFileSystemObserve
        • IFileSystemOpen
      • IEvent
      • IEntry
      • IOption
      • IToken
    • FileSystem
    • VirtualFileSystem
    • MemoryFileSystem
    • EmbeddedFileSystem
    • HttpFileSystem
    • Decoration
    • IFileProvider
    • Utility
      • DisposeList
      • FileScanner
      • VisitTree
      • File Operation
  • Lexical.FileProvider
    • Introduction
    • Package
    • Package.Abstractions
    • Root
    • Zip
    • Dll
    • SharpCompress
    • SharpZipLib
    • FileScanner
    • Utils
  • Lexical.Localization
    • Introduction
    • Tutorial
    • Asset
      • IAsset
      • IStringAsset
    • Line
      • ILine
      • ILineFactory
      • ILineRoot
      • ILineFormat
      • ILineLogger
      • LineComparer
    • File
      • ILineReader
      • ILineWriter
      • Ini
      • Json
      • Xml
      • Resx
      • Resources
    • Miscellaneous
      • Plurality
      • ICulturePolicy
      • IStringFormat
      • Dependency Injection
    • Practices
      • Class Library
      • Class Library DI
      • Class Library DI opt.
  • Lexical.Utilities
    • Introduction
    • UnicodeString
    • FileScanner
    • Permutation
    • Tuples
    • StructList

Lexical.FileProvider.SharpZipLib

Lexical.FileProviders.SharpZipLib is a class library that wraps SharpZipLib into IFileProvider.

The new BZip2FileProvider(string, string) constructs a file provider that reads .bzip2 content. Other class is LzwFileProvider. Content can be read concurrently as file provider opens new file handles as needed.

BZip2FileProvider fileProvider_bzip2 = new BZip2FileProvider("mydata.tar.bzip2", "mydata.tar");
LzwFileProvider fileProvider_lzw = new LzwFileProvider("mydata.tar.Z", "mydata.tar");

Another constructor variant is new BZip2FileProvider(byte[], string).

// Read data
byte[] data;
using (Stream stream = new FileStream("mydata.tar.bzip2", FileMode.Open))
    data = FileUtils.ReadFully(stream);

// Use stream as zip file.
BZip2FileProvider fileProvider = new BZip2FileProvider(data, "mydata.tar");

File provider must be disposed after use.

fileProvider.Dispose();

Belated disposing can be added to the file provider. The disposable will be disposed once the fileprovider and all its streams are closed.

// Create file provider
BZip2FileProvider fileProvider = new BZip2FileProvider("mydata.tar.bzip2", "mydata.tar");
// Add disposable for belated dispose
fileProvider.AddBelatedDispose(new _Disposable_());
// Open stream
Stream s = fileProvider
        .GetFileInfo("mydata.tar")
        .CreateReadStream();
// Dispose file provider
fileProvider.Dispose();
// Dispose the open stream  --  _Disposable_ is disposed here.
s.Dispose();

Package Loader

Lexical.FileProvider.PackageLoader.BZip2 and .Lzw can be used as a component of PackageFileLoader, making it possible to drill down into archive files recursively.

// Create root file provider
PhysicalFileProvider root = new PhysicalFileProvider(Directory.GetCurrentDirectory());

// Create package options
IPackageFileProviderOptions options =
    new PackageFileProviderOptions()
    .AddPackageLoaders(
        Lexical.FileProvider.PackageLoader.Tar.Singleton,
        Lexical.FileProvider.PackageLoader.BZip2.Singleton,
        Lexical.FileProvider.PackageLoader.Lzw.Singleton
    );

// Create package file provider
IPackageFileProvider fileProvider = new PackageFileProvider(root, options).AddDisposable(root);

// Read compressed file
using (Stream document = fileProvider.GetFileInfo("document.txt.Z/document.txt").CreateReadStream())
{
    byte[] data = FileUtils.ReadFully(document);
    string text = Encoding.UTF8.GetString(data);
    Console.WriteLine(text);
}

Links

  • Lexical.FileProvider.SharpZipLib (NuGet, Git)
  • Lexical.FileProvider.Abstractions (NuGet)
    • IPackageLoader
  • Microsoft.Extensions.FileProviders.Abstractions (NuGet)
    • IFileProvider
  • SharpZipLib
Back to top Copyright © 2015-2020 Toni Kalajainen