• 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.SharpCompress

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

The new _ZipFileProvider(string) constructs a file provider that reads .zip contents. Other classes are RarFileProvider, _7zFileProvider, TarFileProvider and GZipFileProvider. Content can be read concurrently as file provider opens new file handles as needed.

_ZipFileProvider fileProvider_zip = new _ZipFileProvider("mydata.zip");
RarFileProvider fileProvider_rar = new RarFileProvider("mydata.rar");
TarFileProvider fileProvider_tar = new TarFileProvider("mydata.tar");
_7zFileProvider fileProvider_7z = new _7zFileProvider("mydata.7z");
GZipFileProvider fileProvider_gz = new GZipFileProvider("mydata.tar.gz", "mydata.tar");

Another constructor argument is new _ZipFileProvider(Stream). Note however, that unlike file based constructor, this stream based constructor has only one file pointer, content can be accessed only by one thread at a time. Concurrent threads have to wait.

// Open some stream
Stream stream = new FileStream("mydata.zip", FileMode.Open);

// Use stream as zip file.
_ZipFileProvider fileProvider = new _ZipFileProvider(stream).AddDisposable(stream);

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
_ZipFileProvider fileProvider = new _ZipFileProvider("mydata.zip");
// Add disposable for belated dispose
fileProvider.AddBelatedDispose(new _Disposable_());
// Open stream
Stream s = fileProvider
        .GetFileInfo("Lexical.Localization.Tests.dll")
        .CreateReadStream();
// Dispose file provider
fileProvider.Dispose();
// Dispose the open stream  --  _Disposable_ is disposed here.
s.Dispose();

Package Loader

Lexical.FileProvider.PackageLoader._Zip, .Rar, ._7z, .Tar and .GZip 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._Zip.Singleton, 
        Lexical.FileProvider.PackageLoader.Rar.Singleton,
        Lexical.FileProvider.PackageLoader._7z.Singleton,
        Lexical.FileProvider.PackageLoader.Tar.Singleton,
        Lexical.FileProvider.PackageLoader.GZip.Singleton
    );

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

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

Links

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