.net - Is there a library to unzip .Z files using VB.NET? -


i need unzip list of .z files kept in folder using visual basic.net. example, consider there folder c:\filestobeunzipped. inside folder there 5 6 files .z extension. need unzip these .z files , save unzipped files in folder c:\unzippeddatafiles.

is possible in vb.net? there free component or class library achive it?

i ran issue well, needed decompress *.z file

please note, not original author of however, wanted provide copy of source on stack overflow in case original site no longer available.

the original post can found here

here example of how use class:

using system; using system.io;  using ebixio.lzw;  class mainclass {     public static void main(string[] args)     {         byte[] buffer = new byte[4096];         string outfile = path.getfilenamewithoutextension(args[0]);          using (stream instream = new lzwinputstream(file.openread(args[0])))         using (filestream outstream = file.create(outfile)) {             int read;             while ((read = instream.read(buffer, 0, buffer.length)) > 0) {                 outstream.write(buffer, 0, read);             }         }     } } 

here modifed version of static method made. allows choosing *.z file location , path extract to:

public static void lzwuncompress(this string path, string unzippedpath) {             byte[] buffer = new byte[4096];             //string outfile = path.getfilenamewithoutextension(path);              using (stream instream = new ebixio.lzw.lzwinputstream(file.openread(path)))             using (filestream outstream = file.create(unzippedpath)) {                 int read;                 while ((read = instream.read(buffer, 0, buffer.length)) > 0) {                     outstream.write(buffer, 0, read);                 }             }         } 

an example using static method:

savepath.lzwuncompress(unzippedpath); 

add class file project:

// lzwinputstream.cs // // copyright (c) 2009 gabriel burca // // program free software; can redistribute and/or // modify under terms of gnu general public license // published free software foundation; either version 2 // of license, or (at option) later version. // // program distributed in hope useful, // without warranty; without implied warranty of // merchantability or fitness particular purpose.  see // gnu general public license more details. // // should have received copy of gnu general public license // along program; if not, write free software // foundation, inc., 59 temple place - suite 330, boston, ma  02111-1307, usa. // // linking library statically or dynamically other modules // making combined work based on library.  thus, terms , // conditions of gnu general public license cover whole // combination. //  using system; using system.io;  namespace ebixio.lzw {      /// <summary>     /// class contains constants used lzw     /// </summary>     sealed public class lzwconstants {         /// <summary>         /// magic number found @ start of lzw header: 0x1f 0x9d         /// </summary>         public const int magic = 0x1f9d;          /// <summary>         /// maximum number of bits per code         /// </summary>         public const int max_bits = 16;          /* 3rd header byte:          * bit 0..4 number of compression bits          * bit 5    extended header          * bit 6    free          * bit 7    block mode          */          /// <summary>         /// mask 'number of compression bits'         /// </summary>         public const int bit_mask = 0x1f;          /// <summary>         /// indicates presence of fourth header byte         /// </summary>         public const int extended_mask = 0x20;         //public const int free_mask      = 0x40;          /// <summary>         /// reserved bits         /// </summary>         public const int reserved_mask = 0x60;          /// <summary>         /// block compression: if table full , compression rate dropping,         /// clear dictionary.         /// </summary>         public const int block_mode_mask = 0x80;          /// <summary>         /// lzw file header size (in bytes)         /// </summary>         public const int hdr_size = 3;          /// <summary>         /// initial number of bits per code         /// </summary>         public const int init_bits = 9;          lzwconstants() {         }     }       /// <summary>     /// filter stream used decompress lzw format stream.     /// specifically, stream uses lzc compression method.     /// file format associated .z file extension.     ///     /// see http://en.wikipedia.org/wiki/compress     /// see http://wiki.wxwidgets.org/development:_z_file_format     ///     /// file header consists of 3 (or optionally 4) bytes. first 2 bytes     /// contain magic marker "0x1f 0x9d", followed byte of flags.     ///     /// based on java code ronald tschalar, in turn based on unlzw.c     /// code in gzip package.     /// </summary>     /// <example> sample shows how unzip compressed file     /// <code>     /// using system;     /// using system.io;     ///     /// using ebixio.lzw;     ///     /// class mainclass     /// {     ///     public static void main(string[] args)     ///     {     ///         byte[] buffer = new byte[4096];     ///         using (stream instream = new lzwinputstream(file.openread(args[0])))     ///         using (filestream outstream = file.create(path.getfilenamewithoutextension(args[0]))) {     ///             int read;     ///             while ((read = instream.read(buffer, 0, buffer.length)) > 0) {     ///                 outstream.write(buffer, 0, read);     ///             }     ///         }     ///     }     /// }     /// </code>     /// </example>     public class lzwinputstream : stream {         byte[] 1 = new byte[1];         bool headerparsed = false;          // string table stuff         private const int tbl_clear = 0x100;         private const int tbl_first = tbl_clear + 1;          private int[] tabprefix;         private byte[] tabsuffix;         private int[] zeros = new int[256];         private byte[] stack;          // various state         private bool blockmode;         private int nbits;         private int maxbits;         private int maxmaxcode;         private int maxcode;         private int bitmask;         private int oldcode;         private byte finchar;         private int stackp;         private int freeent;          // input buffer         private byte[] data = new byte[10000];         private int bitpos = 0, end = 0, got = 0;         private bool eof = false;         private const int = 64;          /// <summary>         /// creates lzwinputstream         /// </summary>         /// <param name="baseinputstream">         /// stream read compressed data (baseinputstream lzw format)         /// </param>         public lzwinputstream(stream baseinputstream) {             this.baseinputstream = baseinputstream;         }          /// <summary>         /// see <see cref="system.io.stream.readbyte"/>         /// </summary>         /// <returns></returns>         public override int readbyte() {             int b = read(one, 0, 1);             if (b == 1)                 return (one[0] & 0xff);             else                 return -1;         }          /// <summary>         /// moves unread data in buffer beginning , resets         /// pointers.         /// </summary>         /// <param name="bitpos"></param>         /// <returns></returns>         private int resetbuf(int bitpos) {             int pos = bitpos >> 3;             array.copy(data, pos, data, 0, end - pos);             end -= pos;             return 0;         }           private void fill() {             got = baseinputstream.read(data, end, data.length - 1 - end);             if (got > 0) end += got;         }           private void parseheader() {             headerparsed = true;              byte[] hdr = new byte[lzwconstants.hdr_size];              int result = baseinputstream.read(hdr, 0, hdr.length);              // check magic marker             if (result < 0) throw new applicationexception("failed read lzw header");             if (hdr[0] != (lzwconstants.magic >> 8) || hdr[1] != (lzwconstants.magic & 0xff)) {                 throw new applicationexception(string.format(                     "wrong lzw header. magic bytes don't match. 0x{0:x2} 0x{1:x2}",                     hdr[0], hdr[1]));             }              // check 3rd header byte             blockmode = (hdr[2] & lzwconstants.block_mode_mask) > 0;             maxbits = hdr[2] & lzwconstants.bit_mask;              if (maxbits > lzwconstants.max_bits) {                 throw new applicationexception("stream compressed " + maxbits +                     " bits, decompression can handle " +                     lzwconstants.max_bits + " bits.");             }              if ((hdr[2] & lzwconstants.reserved_mask) > 0) {                 throw new applicationexception("unsupported bits set in header.");             }              // initialize variables             maxmaxcode = 1 << maxbits;             nbits = lzwconstants.init_bits;             maxcode = (1 << nbits) - 1;             bitmask = maxcode;             oldcode = -1;             finchar = 0;             freeent = blockmode ? tbl_first : 256;              tabprefix = new int[1 << maxbits];             tabsuffix = new byte[1 << maxbits];             stack = new byte[1 << maxbits];             stackp = stack.length;              (int idx = 255; idx >= 0; idx--)                 tabsuffix[idx] = (byte)idx;         }          #region stream overrides          /// <summary>         /// reads decompressed data provided buffer byte array         /// </summary>         /// <param name ="buffer">         /// array read , decompress data         /// </param>         /// <param name ="offset">         /// offset indicating data should placed         /// </param>         /// <param name ="count">         /// number of bytes decompress         /// </param>         /// <returns>the number of bytes read. 0 signals end of stream</returns>         public override int read(byte[] buffer, int offset, int count) {             if (!headerparsed) parseheader();              if (eof) return -1;             int start = offset;              /* using local copies of various variables speeds things              * 30% in java! performance not tested in c#.              */             int[] ltabprefix = tabprefix;             byte[] ltabsuffix = tabsuffix;             byte[] lstack = stack;             int lnbits = nbits;             int lmaxcode = maxcode;             int lmaxmaxcode = maxmaxcode;             int lbitmask = bitmask;             int loldcode = oldcode;             byte lfinchar = finchar;             int lstackp = stackp;             int lfreeent = freeent;             byte[] ldata = data;             int lbitpos = bitpos;               // empty stack if stuff still left             int ssize = lstack.length - lstackp;             if (ssize > 0) {                 int num = (ssize >= count) ? count : ssize;                 array.copy(lstack, lstackp, buffer, offset, num);                 offset += num;                 count -= num;                 lstackp += num;             }              if (count == 0) {                 stackp = lstackp;                 return offset - start;             }               // loop, filling local buffer until enough data has been decompressed         mainloop: {                 if (end < extra) fill();                  int bitin = (got > 0) ? (end - end % lnbits) << 3 :                                         (end << 3) - (lnbits - 1);                  while (lbitpos < bitin) {                     #region                     // handle 1-byte reads correctly                     if (count == 0) {                         nbits = lnbits;                         maxcode = lmaxcode;                         maxmaxcode = lmaxmaxcode;                         bitmask = lbitmask;                         oldcode = loldcode;                         finchar = lfinchar;                         stackp = lstackp;                         freeent = lfreeent;                         bitpos = lbitpos;                          return offset - start;                     }                      // check code-width expansion                     if (lfreeent > lmaxcode) {                         int nbytes = lnbits << 3;                         lbitpos = (lbitpos - 1) +                         nbytes - (lbitpos - 1 + nbytes) % nbytes;                          lnbits++;                         lmaxcode = (lnbits == maxbits) ? lmaxmaxcode :                                                         (1 << lnbits) - 1;                          lbitmask = (1 << lnbits) - 1;                         lbitpos = resetbuf(lbitpos);                         goto mainloop;                     }                     #endregion                      #region b                     // read next code                     int pos = lbitpos >> 3;                     int code = (((ldata[pos] & 0xff) |                         ((ldata[pos + 1] & 0xff) << 8) |                         ((ldata[pos + 2] & 0xff) << 16)) >>                         (lbitpos & 0x7)) & lbitmask;                      lbitpos += lnbits;                      // handle first iteration                     if (loldcode == -1) {                         if (code >= 256) throw new applicationexception("corrupt input: " + code + " > 255");                          lfinchar = (byte)(loldcode = code);                         buffer[offset++] = lfinchar;                         count--;                         continue;                     }                      // handle clear code                     if (code == tbl_clear && blockmode) {                         array.copy(zeros, 0, ltabprefix, 0, zeros.length);                         lfreeent = tbl_first - 1;                          int nbytes = lnbits << 3;                         lbitpos = (lbitpos - 1) + nbytes - (lbitpos - 1 + nbytes) % nbytes;                         lnbits = lzwconstants.init_bits;                         lmaxcode = (1 << lnbits) - 1;                         lbitmask = lmaxcode;                          // code tables reset                          lbitpos = resetbuf(lbitpos);                         goto mainloop;                     }                     #endregion                      #region c                     // setup                     int incode = code;                     lstackp = lstack.length;                      // handle kwk case                     if (code >= lfreeent) {                         if (code > lfreeent) {                             throw new applicationexception("corrupt input: code=" + code +                                 ", freeent=" + lfreeent);                         }                          lstack[--lstackp] = lfinchar;                         code = loldcode;                     }                      // generate output characters in reverse order                     while (code >= 256) {                         lstack[--lstackp] = ltabsuffix[code];                         code = ltabprefix[code];                     }                      lfinchar = ltabsuffix[code];                     buffer[offset++] = lfinchar;                     count--;                      // , put them out in forward order                     ssize = lstack.length - lstackp;                     int num = (ssize >= count) ? count : ssize;                     array.copy(lstack, lstackp, buffer, offset, num);                     offset += num;                     count -= num;                     lstackp += num;                     #endregion                      #region d                     // generate new entry in table                     if (lfreeent < lmaxmaxcode) {                         ltabprefix[lfreeent] = loldcode;                         ltabsuffix[lfreeent] = lfinchar;                         lfreeent++;                     }                      // remember previous code                     loldcode = incode;                      // if output buffer full, return                     if (count == 0) {                         nbits = lnbits;                         maxcode = lmaxcode;                         bitmask = lbitmask;                         oldcode = loldcode;                         finchar = lfinchar;                         stackp = lstackp;                         freeent = lfreeent;                         bitpos = lbitpos;                          return offset - start;                     }                     #endregion                 }   // while                  lbitpos = resetbuf(lbitpos);              } while (got > 0);  // do..while              nbits = lnbits;             maxcode = lmaxcode;             bitmask = lbitmask;             oldcode = loldcode;             finchar = lfinchar;             stackp = lstackp;             freeent = lfreeent;             bitpos = lbitpos;              eof = true;             return offset - start;         }           /// <summary>         /// gets value indicating whether current stream supports reading         /// </summary>         public override bool canread {             {                 return baseinputstream.canread;             }         }          /// <summary>         /// gets value of false indicating seeking not supported stream.         /// </summary>         public override bool canseek {             {                 return false;             }         }          /// <summary>         /// gets value of false indicating stream not writeable.         /// </summary>         public override bool canwrite {             {                 return false;             }         }          /// <summary>         /// value representing length of stream in bytes.         /// throws notsupportedexception when attempting length.         /// </summary>         /// <exception cref="notsupportedexception">attempting length</exception>         public override long length {             {                 throw new notsupportedexception("lzwinputstream length not supported");             }         }          /// <summary>         /// current position within stream.         /// throws notsupportedexception when attempting set position.         /// </summary>         /// <exception cref="notsupportedexception">attempting set position</exception>         public override long position {             {                 return baseinputstream.position;             }             set {                 throw new notsupportedexception("lzwinputstream position not supported");             }         }          /// <summary>         /// flushes baseinputstream         /// </summary>         public override void flush() {             baseinputstream.flush();         }          /// <summary>         /// sets position within current stream         /// throws notsupportedexception         /// </summary>         /// <param name="offset">the relative offset seek to.</param>         /// <param name="origin">the <see cref="seekorigin"/> defining seek from.</param>         /// <returns>the new position in stream.</returns>         /// <exception cref="notsupportedexception">any access</exception>         public override long seek(long offset, seekorigin origin) {             throw new notsupportedexception("lzwinputstream seek not supported");         }          /// <summary>         /// set length of current stream         /// throws notsupportedexception         /// </summary>         /// <param name="value">the new length value stream.</param>         /// <exception cref="notsupportedexception">any access</exception>         public override void setlength(long value) {             throw new notsupportedexception("lzwinputstream setlength not supported");         }          /// <summary>         /// writes sequence of bytes stream , advances current position         /// method throws notsupportedexception         /// </summary>         /// <param name="buffer">thew buffer containing data write.</param>         /// <param name="offset">the offset of first byte write.</param>         /// <param name="count">the number of bytes write.</param>         /// <exception cref="notsupportedexception">any access</exception>         public override void write(byte[] buffer, int offset, int count) {             throw new notsupportedexception("lzwinputstream write not supported");         }          /// <summary>         /// writes 1 byte current stream , advances current position         /// throws notsupportedexception         /// </summary>         /// <param name="value">the byte write.</param>         /// <exception cref="notsupportedexception">any access</exception>         public override void writebyte(byte value) {             throw new notsupportedexception("lzwinputstream writebyte not supported");         }          /// <summary>         /// entry point begin asynchronous write.  throws notsupportedexception.         /// </summary>         /// <param name="buffer">the buffer write data from</param>         /// <param name="offset">offset of first byte write</param>         /// <param name="count">the maximum number of bytes write</param>         /// <param name="callback">the method called when asynchronous write operation completed</param>         /// <param name="state">a user-provided object distinguishes particular asynchronous write request other requests</param>         /// <returns>an <see cref="system.iasyncresult">iasyncresult</see> references asynchronous write</returns>         /// <exception cref="notsupportedexception">any access</exception>         public override iasyncresult beginwrite(byte[] buffer, int offset, int count, asynccallback callback, object state) {             throw new notsupportedexception("lzwinputstream beginwrite not supported");         }          /// <summary>         /// closes input stream.  when <see cref="isstreamowner"></see>         /// true underlying stream closed.         /// </summary>         public override void close() {             if (!isclosed) {                 isclosed = true;                 if (isstreamowner) {                     baseinputstream.close();                 }             }         }          #endregion           /// <summary>         /// get/set flag indicating ownership of underlying stream.         /// when flag true <see cref="close"/> close underlying stream also.         /// </summary>         /// <remarks>         /// default value true.         /// </remarks>         public bool isstreamowner {             { return isstreamowner; }             set { isstreamowner = value; }         }          /// <summary>         /// base stream inflater reads from.         /// </summary>         protected stream baseinputstream;          /// <summary>         /// flag indicating wether instance has been closed or not.         /// </summary>         bool isclosed;          /// <summary>         /// flag indicating wether instance designated stream owner.         /// when closing if flag true underlying stream closed.         /// </summary>         bool isstreamowner = true;     } } 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -