logo

Unique class for compression/decompression file. Represents a Zip file. More...

+ Inheritance diagram for NoteFly.ZipStorer:

Classes

struct  ZipFileEntry
 Represents an entry in Zip file directory More...
 

Public Types

enum  Compression : ushort { Store = 0, Deflate = 8 }
 Compression method enumeration More...
 

Public Member Functions

void AddFile (Compression compressionmethod, string pathname, string filenameInZip, string zipfilecomment)
 Add full contents of a file into the Zip storage More...
 
void AddStream (Compression compressionmethod, string filenameInZip, Stream source, DateTime modTime, string zipfilecomment)
 Add full contents of a stream into the Zip storage More...
 
void Close ()
 Updates central directory (if pertinent) and close the Zip storage More...
 
List< ZipFileEntryReadCentralDir ()
 Read all the file records in the central directory More...
 
bool ExtractFile (ZipFileEntry zfe, string filename)
 Copy the contents of a stored file into a physical file More...
 
bool ExtractFile (ZipFileEntry zfe, Stream stream)
 Copy the contents of a stored file into an opened stream More...
 
void Dispose ()
 Closes the Zip file stream More...
 

Static Public Member Functions

static ZipStorer Create (string zipfilename, string zipcomment)
 Method to create a new storage file More...
 
static ZipStorer Create (Stream zipstream, string zipcomment)
 Method to create a new zip storage in a stream. More...
 
static ZipStorer Open (string zipfile, FileAccess zipfileaccess)
 Method to open an existing storage file. More...
 
static ZipStorer Open (Stream zipstream, FileAccess zipfileaccess)
 Method to open an existing storage from stream More...
 
static bool RemoveEntries (ref ZipStorer zip, List< ZipFileEntry > zfes)
 Removes one of many files in storage. It creates a new Zip file. More...
 

Public Attributes

bool EncodeUTF8 = false
 True if UTF8 encoding for filename and comments, false if default (CP 437) More...
 
bool ForceDeflating = false
 Force deflate algotithm even if it inflates the stored file. Off by default. More...
 

Private Member Functions

uint GetFileOffset (uint headerOffset)
 Calculate the file offset by reading the corresponding local header More...
 
void WriteLocalHeader (ref ZipFileEntry zfe)
  More...
 
void WriteCentralDirRecord (ZipFileEntry zfe)
 Central directory's File header: central file header signature 4 bytes (0x02014b50) version made by 2 bytes version needed to extract 2 bytes general purpose bit flag 2 bytes compression method 2 bytes last mod file time 2 bytes last mod file date 2 bytes crc-32 4 bytes compressed size 4 bytes uncompressed size 4 bytes filename length 2 bytes extra field length 2 bytes file comment length 2 bytes disk number start 2 bytes internal file attributes 2 bytes external file attributes 4 bytes relative offset of local header 4 bytes More...
 
void WriteEndRecord (uint size, uint offset)
 End of central dir record: end of central dir signature 4 bytes (0x06054b50) number of this disk 2 bytes number of the disk with the start of the central directory 2 bytes total number of entries in the central dir on this disk 2 bytes total number of entries in the central dir 2 bytes size of the central directory 4 bytes offset of start of central directory with respect to the starting disk number 4 bytes zipfile comment length 2 bytes zipfile comment (variable size) More...
 
void Store (ref ZipFileEntry zfe, Stream source)
 Copies all source file into storage file More...
 
uint DateTimeToDosTime (DateTime dtdostime)
  More...
 
DateTime DosTimeToDateTime (uint dtdostime)
  More...
 
void UpdateCrcAndSizes (ref ZipFileEntry zfe)
  More...
 
string NormalizedFilename (string zipfilename)
 Replaces backslashes with slashes to store in zip header. More...
 
bool ReadFileInfo ()
 Reads the end-of-central-directory record. More...
 

Static Private Member Functions

static ZipStorer ()
 Static constructor. Just invoked once in order to create the CRC32 lookup table. More...
 

Private Attributes

List< ZipFileEntryfiles = new List<ZipFileEntry>()
 List of files to store More...
 
string filename
 Filename of storage file More...
 
Stream zipfilestream
 Stream object of storage file More...
 
string comment = string.Empty
 General comment More...
 
byte[] centraldirimage = null
 Central dir image More...
 
ushort existingfiles = 0
 Existing files in zip More...
 
FileAccess fileaccess
 File access for Open method More...
 

Static Private Attributes

static uint[] crctable = null
 Static CRC32 Table More...
 
static Encoding DefaultEncoding = Encoding.GetEncoding(437)
 Default filename encoder More...
 

Detailed Description

Member Enumeration Documentation

Enumerator
Store 

Uncompressed storage

Deflate 

Deflate compression method

Constructor & Destructor Documentation

static NoteFly.ZipStorer.ZipStorer ( )
staticprivate

Member Function Documentation

void NoteFly.ZipStorer.AddFile ( Compression  compressionmethod,
string  pathname,
string  filenameInZip,
string  zipfilecomment 
)
Parameters
compressionmethodCompression method
pathnameFull path of file to add to Zip storage
filenameInZipFilename and path as desired in Zip directory
zipfilecommentComment for stored file
void NoteFly.ZipStorer.AddStream ( Compression  compressionmethod,
string  filenameInZip,
Stream  source,
DateTime  modTime,
string  zipfilecomment 
)
Parameters
compressionmethodCompression method
filenameInZipFilename and path as desired in Zip directory
sourceStream object containing the data to store in Zip
modTimeModification time of the data to store
commentComment for stored file
void NoteFly.ZipStorer.Close ( )

This is a required step, unless automatic dispose is used

static ZipStorer NoteFly.ZipStorer.Create ( string  zipfilename,
string  zipcomment 
)
static
Parameters
zipfilenameFull path of Zip file to create
zipcommentGeneral comment for Zip file
Returns
A valid ZipStorer object
static ZipStorer NoteFly.ZipStorer.Create ( Stream  zipstream,
string  zipcomment 
)
static
Parameters
streamThe zip file stream.
commentThe zip file comment.
Returns
A valid ZipStorer object
uint NoteFly.ZipStorer.DateTimeToDosTime ( DateTime  dtdostime)
private

DOS Date and time: MS-DOS date. The date is a packed value with the following format. Bits Description 0-4 Day of the month (1–31) 5-8 Month (1 = January, 2 = February, and so on) 9-15 Year offset from 1980 (add 1980 to get actual year) MS-DOS time. The time is a packed value with the following format. Bits Description 0-4 Second divided by 2 5-10 Minute (0–59) 11-15 Hour (0–23 on a 24-hour clock)

Parameters
dtdostime
Returns
void NoteFly.ZipStorer.Dispose ( )
DateTime NoteFly.ZipStorer.DosTimeToDateTime ( uint  dtdostime)
private
Parameters
dtdostime
Returns
bool NoteFly.ZipStorer.ExtractFile ( ZipFileEntry  zfe,
string  filename 
)
Parameters
zfeEntry information of file to extract
filenameName of file to store uncompressed data
Returns
True if success, false if not.

Unique compression methods are Store and Deflate

bool NoteFly.ZipStorer.ExtractFile ( ZipFileEntry  zfe,
Stream  stream 
)
Parameters
zfeEntry information of file to extract
streamStream to store the uncompressed data
Returns
True if success, false if not.

Unique compression methods are Store and Deflate

uint NoteFly.ZipStorer.GetFileOffset ( uint  headerOffset)
private
Parameters
_headerOffsetFile pointer offset
Returns
string NoteFly.ZipStorer.NormalizedFilename ( string  zipfilename)
private
Parameters
zipfilename
Returns
static ZipStorer NoteFly.ZipStorer.Open ( string  zipfile,
FileAccess  zipfileaccess 
)
static
Parameters
filenameFull path of Zip file to open
accessFile access mode as used in FileStream constructor
Returns
A valid ZipStorer object
static ZipStorer NoteFly.ZipStorer.Open ( Stream  zipstream,
FileAccess  zipfileaccess 
)
static
Parameters
streamAlready opened stream with zip contents
accessFile access mode for stream operations
Returns
A valid ZipStorer object
List<ZipFileEntry> NoteFly.ZipStorer.ReadCentralDir ( )
Returns
List of all entries in directory
bool NoteFly.ZipStorer.ReadFileInfo ( )
private
Returns
static bool NoteFly.ZipStorer.RemoveEntries ( ref ZipStorer  zip,
List< ZipFileEntry zfes 
)
static
Parameters
zipReference to the current Zip object
zfesList of Entries to remove from storage
Returns
True if success, false if not

This method only works for storage of type FileStream

void NoteFly.ZipStorer.Store ( ref ZipFileEntry  zfe,
Stream  source 
)
private
Parameters
zfe
source
void NoteFly.ZipStorer.UpdateCrcAndSizes ( ref ZipFileEntry  zfe)
private

CRC32 algorithm The 'magic number' for the CRC is 0xdebb20e3. The proper CRC pre and post conditioning is used, meaning that the CRC register is pre-conditioned with all ones (a starting value of 0xffffffff) and the value is post-conditioned by taking the one's complement of the CRC residual. If bit 3 of the general purpose flag is set, this field is set to zero in the local header and the correct value is put in the data descriptor and in the central directory.

Parameters
zfe
void NoteFly.ZipStorer.WriteCentralDirRecord ( ZipFileEntry  zfe)
private

filename (variable size) extra field (variable size) file comment (variable size)

void NoteFly.ZipStorer.WriteEndRecord ( uint  size,
uint  offset 
)
private
Parameters
size
offset
void NoteFly.ZipStorer.WriteLocalHeader ( ref ZipFileEntry  zfe)
private

Local file header: local file header signature 4 bytes (0x04034b50) version needed to extract 2 bytes general purpose bit flag 2 bytes compression method 2 bytes last mod file time 2 bytes last mod file date 2 bytes crc-32 4 bytes compressed size 4 bytes uncompressed size 4 bytes filename length 2 bytes extra field length 2 bytes

filename (variable size) extra field (variable size)

Parameters
zfe

Member Data Documentation

byte [] NoteFly.ZipStorer.centraldirimage = null
private
string NoteFly.ZipStorer.comment = string.Empty
private
uint [] NoteFly.ZipStorer.crctable = null
staticprivate
Encoding NoteFly.ZipStorer.DefaultEncoding = Encoding.GetEncoding(437)
staticprivate
bool NoteFly.ZipStorer.EncodeUTF8 = false
ushort NoteFly.ZipStorer.existingfiles = 0
private
FileAccess NoteFly.ZipStorer.fileaccess
private
string NoteFly.ZipStorer.filename
private
List<ZipFileEntry> NoteFly.ZipStorer.files = new List<ZipFileEntry>()
private
bool NoteFly.ZipStorer.ForceDeflating = false
Stream NoteFly.ZipStorer.zipfilestream
private