All Projects → jaime-olivares → Zipstorer

jaime-olivares / Zipstorer

Licence: mit
A Pure C# Class to Store Files in Zip

Programming Languages

csharp
926 projects

Projects that are alternatives of or similar to Zipstorer

Megaapiclient
MegaApiClient is a C# .Net library to access http://mega.co.nz / http://mega.nz cloud storage and file hosting service.
Stars: ✭ 151 (+8.63%)
Mutual labels:  netstandard, nuget, netcore, mono
Colore
A powerful C# library for Razer Chroma's SDK
Stars: ✭ 121 (-12.95%)
Mutual labels:  netstandard, nuget, netcore
Standard.licensing
Easy-to-use licensing library for .NET Framework, Mono, .NET Core, and Xamarin products
Stars: ✭ 239 (+71.94%)
Mutual labels:  netstandard, netcore, mono
Eventflow
Async/await first CQRS+ES and DDD framework for .NET
Stars: ✭ 1,932 (+1289.93%)
Mutual labels:  async-await, netstandard, nuget
dark-sky-core
A .NET Standard Library for using the Dark Sky API.
Stars: ✭ 55 (-60.43%)
Mutual labels:  nuget, netcore, netstandard
Nativexplatform
Akeeba Portable Tools (cross-platform) - Desktop utilities for use with Akeeba Backup and Akeeba Solo
Stars: ✭ 17 (-87.77%)
Mutual labels:  zip, cross-platform, mono
Portable-WebDAV-Library
Moved to codeberg.org - https://codeberg.org/DecaTec/Portable-WebDAV-Library - The Portable WebDAV Library is a strongly typed, async WebDAV client library which is fully compliant to RFC 4918, RFC 4331 and "Additional WebDAV Collection Properties". It is implemented as .NETStandard 1.1 library in oder to be used on any platform supporting .NETS…
Stars: ✭ 45 (-67.63%)
Mutual labels:  mono, netcore, netstandard
Opengl.net
Modern OpenGL bindings for C#.
Stars: ✭ 473 (+240.29%)
Mutual labels:  netstandard, nuget, mono
unzip
Tiny unzip helper class for .NET 3.5 Client Profile and Mono 2.10, written in pure C#.
Stars: ✭ 25 (-82.01%)
Mutual labels:  compression, nuget, zip
Fo Dicom
Fellow Oak DICOM for .NET, .NET Core, Universal Windows, Android, iOS, Mono and Unity
Stars: ✭ 674 (+384.89%)
Mutual labels:  nuget, netcore, mono
Abotx
Cross Platform C# Web crawler framework, headless browser, parallel crawler. Please star this project! +1.
Stars: ✭ 63 (-54.68%)
Mutual labels:  netstandard, cross-platform, netcore
Mailmergelib
MailMergeLib is a mail message client library which provides comfortable mail merge capabilities for text, inline images and attachments, as well as good throughput and fault tolerance for sending mail messages.
Stars: ✭ 97 (-30.22%)
Mutual labels:  netstandard, netcore
Sharpcompress
SharpCompress is a fully managed C# library to deal with many compression types and formats.
Stars: ✭ 1,397 (+905.04%)
Mutual labels:  zip, compression
Raft.net
Implementation of RAFT distributed consensus algorithm among TCP Peers on .NET / .NETStandard / .NETCore / dotnet
Stars: ✭ 112 (-19.42%)
Mutual labels:  netstandard, netcore
Expressive
Expressive is a cross-platform expression parsing and evaluation framework. The cross-platform nature is achieved through compiling for .NET Standard so it will run on practically any platform.
Stars: ✭ 113 (-18.71%)
Mutual labels:  netstandard, cross-platform
Shapes
📐 Net standard geometry/shape manipulation library, can be used to merge / split shapes
Stars: ✭ 95 (-31.65%)
Mutual labels:  netstandard, cross-platform
Spidereye
Cross platform .Net Core applications with a webview UI
Stars: ✭ 113 (-18.71%)
Mutual labels:  cross-platform, netcore
Reversemarkdown Net
ReverseMarkdown.Net is a Html to Markdown converter library in C#. Conversion is very reliable since HtmlAgilityPack (HAP) library is used for traversing the Html DOM
Stars: ✭ 116 (-16.55%)
Mutual labels:  netstandard, netcore
Watsonwebserver
Watson is the fastest, easiest way to build scalable RESTful web servers and services in C#.
Stars: ✭ 125 (-10.07%)
Mutual labels:  stream, nuget
Nlua
Bridge between Lua and the .NET.
Stars: ✭ 1,326 (+853.96%)
Mutual labels:  netstandard, netcore

ZipStorer

A Pure C# class for storing files in Zip format

NuGet github Build Status

ZipStorer is a minimalistic cross-platform .net class to create Zip files and store/retrieve files to/from it, by using the Deflate algorithm. No other compression methods supported.

Advantages and usage

ZipStorer has the following advantages:

  • It is a short and monolithic C# class that can be embedded as source code in any project (1 source file)
  • No Interop calls, increments portability to Mono and other non-Windows platforms
  • Can also be implemented with Mono, .NET Compact Framework and .Net Standard 2.0+
  • Async methods for storing and extracting files (only for .Net Framework 4.5+ and .Net Standard 2.0+)
  • NEW: Support for Zip64 (file sizes > 4GB)
  • UTF8 Encoding support and ePUB compatibility
  • Available as a nuget package

Using the code

The ZipStorer class is the unique one needed to create the zip file. It contains a nested structure (ZipFileEntry) for collecting each directory entry. The class has been declared inside the System.IO namespace.

There is no default constructor. There are two ways to construct a new ZipStorer instance, depending on specific needs: use either the Create() or the Open() static method. To create a new Zip file, use the Create() method like this:

ZipStorer zip = ZipStorer.Create(filename, comment);  // file-oriented version
ZipStorer zip = ZipStorer.Create(stream, comment);  // stream-oriented version

It is required to specify the full path for the new zip file, or pass a valid stream, and optionally add a comment. For opening an existing zip file for appending, the Open() method is required, like the following:

ZipStorer zip = ZipStorer.Open(filename, fileaccess);  // file-oriented version
ZipStorer zip = ZipStorer.Open(stream, fileaccess);  // stream-oriented version

Where fileaccess should be of type System.IO.FileAccess enumeration type. Also, as now ZipStorer is derived from IDisposable interface, the using keyword can be used to ensure proper disposing of the storage resource:

using (ZipStorer zip = ZipStorer.Create(filename, comment))
{
    // some operations with zip object
    //
}   // automatic close operation here

For adding files into an opened zip storage, there are two available methods:

public void AddFile(ZipStorer.Compress _method, string _pathname, string _filenameInZip, string _comment);
public void AddStream(ZipStorer.Compress _method, string _filenameInZip, Stream _source, DateTime _modTime, string _comment);

The first method allows adding an existing file to the storage. The first argument receives the compression method; it can be Store or Deflate enum values. The second argument admits the physical path name, the third one allows to change the path or file name to be stored in the Zip, and the last argument inserts a comment in the storage. Notice that the folder path in the _pathname argument is not saved in the Zip file. Use the _filenameInZip argument instead to specify the folder path and filename. It can be expressed with both slashes or backslashes.

The second method allows adding data from any kind of stream object derived from the System.IO.Stream class. Internally, the first method opens a FileStream and calls the second method.

Finally, it is required to close the storage with the Close() method. This will save the central directory information too. Alternatively, the Dispose() method can be used.

Sample application

The provided sample application is a Winforms project. It will ask for files and store the path names in a ListBox, along with the operation type: creating or appending, and compression method.

Extracting stored files

For extracting a file, the zip directory shall be read first, by using the ReadCentralDir() method, and then the ExtractFile() method, like in the following minimal sample code:

// Open an existing zip file for reading
ZipStorer zip = ZipStorer.Open(@"c:\data\sample.zip", FileAccess.Read);

// Read the central directory collection
List<ZipStorer.ZipFileEntry> dir = zip.ReadCentralDir();

// Look for the desired file
foreach (ZipStorer.ZipFileEntry entry in dir)
{
    if (Path.GetFileName(entry.FilenameInZip) == "sample.jpg")
    {
        // File found, extract it
        zip.ExtractFile(entry, @"c:\data\sample.jpg");
        break;
    }
}
zip.Close();

Removal of entries

Removal of entries in a zip file is a resource-consuming task. The simplest way is to copy all non-removed files into a new zip storage. The RemoveEntries() static method will do this exactly and will construct the ZipStorer object again. For the sake of efficiency, RemoveEntries() will accept many entry references in a single call, as in the following example:

List<ZipStorer.ZipFileEntry> removeList = new List<ZipStorer.ZipFileEntry>();

foreach (object sel in listBox4.SelectedItems)
{
    removeList.Add((ZipStorer.ZipFileEntry)sel);
}

ZipStorer.RemoveEntries(ref zip, removeList);

File and stream usage

The current release of ZipStorer supports both files and streams for creating and opening a zip storage. Several methods are overloaded for this dual support. The advantage of file-oriented methods is simplicity, since those methods will open or create files internally. On the other hand, stream-oriented methods are more flexible by allowing to manage zip storages in streams different than files. File-oriented methods will invoke internally to equivalent stream-oriented methods. Notice that not all streams will apply, because the library requires the streams to be randomly accessed (CanSeek = true). The RemoveEntries method will work only if the zip storage is a file.

    // File-oriented methods:
    public static ZipStorer Create(string _filename, string _comment);
    public static ZipStorer Open(string _filename, FileAccess _access);
    public ZipFileEntry AddFile(Compression _method, string _pathname, string _filenameInZip, string _comment);
    public bool ExtractFile(ZipFileEntry _zfe, string _filename);
    public static bool RemoveEntries(ref ZipStorer _zip, List<zipfileentry> _zfes);  // No stream-oriented equivalent

    // Stream-oriented methods:
    public static ZipStorer Create(Stream _stream, string _comment, bool _leaveOpen);
    public static ZipStorer Open(Stream _stream, FileAccess _access, bool _leaveOpen);
    public ZipFileEntry AddStream(Compression _method, string _filenameInZip, Stream _source, DateTime _modTime, string _comment);
    public bool ExtractFile(ZipFileEntry _zfe, Stream _stream);

    // Async methods (not available for .Net Framework 2.0):
    public ZipFileEntry AddStreamAsync(Compression _method, string _filenameInZip, Stream _source, DateTime _modTime, string _comment)
    public async Task<bool> ExtractFileAsync(ZipFileEntry _zfe, Stream _stream);

The _leaveOpen argument will prevent the stream to be closed after completing the generation of the zip package.

Filename encoding

Traditionally, the ZIP format supported DOS encoding system (a.k.a. IBM Code Page 437) for filenames in header records, which is a serious limitation for using non-occidental and even some occidental characters. Since 2007, the ZIP format specification was improved to support Unicode's UTF-8 encoding system.

ZipStorer class detects UTF-8 encoding by reading the proper flag in each file's header information. For enforcing filenames to be encoded with UTF-8 system, set the EncodeUTF8 member of ZipStorer class to true. All new filenames added will be encoded with UTF8. Notice this doesn't affect stored file contents at all. Also be aware that Windows Explorer's embedded Zip format facility does not recognize well the UTF-8 encoding system, as WinZip or WinRAR do.

.Net Standard support

Now ZipStorer supports .Net Standard 2.0+ and hence a broad range of platforms.

If developing with Visual Studio Code, the csproj file must reference the nuget package:

  <ItemGroup>
    <PackageReference Include="ZipStorer" Version="*" />
  </ItemGroup>
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].