对ICSharpZipLib的包装类,实现常用数据和文件的压缩

using System;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;

namespace Utility
{
 public delegate void ZipEventHandler( string filename , long compressedSize , long size , bool zipFlag );


 public class ZipReference : IBufferItem <<--请参看前文章.做缓冲处理.
 {
  #region IBufferItem 成员

  private bool m_Used = false;
  public bool Used
  {
   get
   {
    return m_Used;
   }
   set
   {
    m_Used = value;
   }
  }
  private static System.Type     m_Type   = typeof( ZipReference );
  private static BufferStorage m_BufferStorage = new BufferStorage();
  private const int       m_BufferLen  = 10240;

  private byte[]        m_Buffer  = new byte[ m_BufferLen ];
  #endregion
  
  public ZipReference()
  {
  }

  public static byte[] GZipData( byte[] input )
  {
   return GZipData( input , 0  , input.Length );
  }

  public static byte[] GZipData( byte[] input , int offset , int length )
  {
   try
   {
    MemoryStream  m = new MemoryStream( );
    GZipOutputStream s = new GZipOutputStream( m );
    s.Write( input , offset  , length );
    s.Finish();
    s.Close();
    return m.ToArray();
   }
   catch{ return null; }
  }

  public static byte[] GZipData( byte[] header , int hoff , int hlen , byte[] input , int offset , int length )
  {
   try
   {
    MemoryStream m  = new MemoryStream( );
    m.Write( header , hoff , hlen );
    GZipOutputStream s = new GZipOutputStream( m );
    s.Write( input , offset  , length );
    s.Finish();
    s.Close();
    return m.ToArray();
   }
   catch{ return null; }
  }
  
  public static byte[] GZipData( byte[] header , byte[] input , int offset , int length )
  {
   try
   {
    MemoryStream m  = new MemoryStream( );
    m.Write( header , 0 , header.Length );
    GZipOutputStream s = new GZipOutputStream( m );
    s.Write( input , offset  , length );
    s.Finish();
    s.Close();
    return m.ToArray();
   }
   catch{ return null; }
  }

  public static byte[] GZipData( byte header , byte[] input , int offset , int length )
  {
   try
   {
    MemoryStream m  = new MemoryStream( );
    m.WriteByte( header );
    GZipOutputStream s = new GZipOutputStream( m );
    s.Write( input , offset  , length );
    s.Finish();
    s.Close();
    return m.ToArray();
   }
   catch{ return null; }
  }

  public static byte[] GUnzipData( byte[] input  )
  {
   return GUnzipData( input , 0 , input.Length );
  }
  public static byte[] GUnzipData( byte[] input , int offset , int length )
  {
   ZipReference reference = m_BufferStorage.GetUnusedBuffer( m_Type ) as ZipReference;
   try
   {
    MemoryStream m  = new MemoryStream( input , offset , length );
    GZipInputStream s  = new GZipInputStream( m );
    MemoryStream o  = new MemoryStream();
    int read    = 0;
    while( ( read = s.Read( reference.m_Buffer , 0 , m_BufferLen ) ) > 0 )
     o.Write( reference.m_Buffer , 0 , read );
    s.Close();
    return o.ToArray() ;
   }
   catch{ return null; }
   finally
   {
    reference.Used = false;
   }
  }

  public static byte[] ZipData( byte[] header , byte[] input , int offset , int length , string FileName )
  {
   try
   {
    MemoryStream m  = new MemoryStream();
    m.Write( header , 0 , header.Length );
    ZipOutputStream s  = new ZipOutputStream( m );
    ZipEntry entry   = new ZipEntry( FileName );
    s.PutNextEntry( entry );
    s.Write( input , offset  , length );
    s.Finish();
    s.Close();
    return m.ToArray();
   }
   catch{ return null; }
  }

  public static byte[] ZipData( byte header , byte[] input , int offset , int length , string FileName )
  {
   try
   {
    MemoryStream m  = new MemoryStream();
    m.WriteByte( header );
    ZipOutputStream s  = new ZipOutputStream( m );
    ZipEntry entry   = new ZipEntry( FileName );
    s.PutNextEntry( entry );
    s.Write( input , offset  , length );
    s.Finish();
    s.Close();
    return m.ToArray();
   }
   catch{ return null; }
  }

  public static byte[] ZipData( byte[] input , string FileName )
  {
   try
   {
    MemoryStream m  = new MemoryStream();
    ZipOutputStream s  = new ZipOutputStream( m );
    ZipEntry entry   = new ZipEntry( FileName );
    s.PutNextEntry( entry );
    s.Write( input , 0  , input.Length );
    s.Finish();
    s.Close();
    return m.ToArray();
   }
   catch{ return null; }
  }

  public static byte[] ZipData( byte[] input , int offset , int length , string FileName )
  {
   try
   {
    MemoryStream m  = new MemoryStream();
    ZipOutputStream s  = new ZipOutputStream( m );
    ZipEntry entry   = new ZipEntry( FileName );
    s.PutNextEntry( entry );
    s.Write( input , offset  , length );
    s.Finish();
    s.Close();
    return m.ToArray();
   }
   catch{ return null; }
  }

  public static byte[] UnzipData( byte[] input , int offset , int length , string FileName )
  {
   try
   {
    MemoryStream m  = new MemoryStream( input , offset , length );
    ZipInputStream s  = new ZipInputStream( m );
    ZipEntry theEntry;
    while( ( theEntry = s.GetNextEntry() ) != null )
    {
     if( theEntry.Name == FileName )
     {
      int nSize = (int)theEntry.Size;
      if( nSize < 0 )
       return null;
      if( nSize == 0 )
       return new byte[0];
      byte[] rc = new byte[ nSize ];
      int nRead = 0;
      int nTmp = 0;
      while( nRead < nSize )
      {
       nTmp = s.Read( rc , nRead , nSize - nRead );
       if( nTmp < 1 )
       {
        s.Close();
        return null;
       }
       nRead += nTmp;
      }
      s.Close();
      return rc ;
     }
    }
    return null;
   }
   catch{ return null; }
  }
  public static byte[] UnzipData( byte[] input , int offset , int length , int nFileIndex )
  {
   try
   {
    MemoryStream m  = new MemoryStream( input , offset , length );
    ZipInputStream s  = new ZipInputStream( m );
    ZipEntry theEntry = null;
    for( int i = 0 ; i < nFileIndex ; i ++ )
     theEntry = s.GetNextEntry();
    theEntry = s.GetNextEntry();
    if( theEntry == null )
     return null;
    int nSize = (int)theEntry.Size;
    if( nSize < 0 )
     return null;
    if( nSize == 0 )
     return new byte[0];
    byte[] rc = new byte[ nSize ];
    int nRead = 0;
    int nTmp = 0;
    while( nRead < nSize )
    {
     nTmp = s.Read( rc , nRead , nSize - nRead );
     if( nTmp < 1 )
     {
      s.Close();
      return null;
     }
     nRead += nTmp;
    }
    s.Close();
    return rc ;
   }
   catch{ return null; }
  }

  public static void UnzipFiles( string strZipFile , string strPath , bool createSubDir )
  {
   ZipInputStream   s = null;
   ZipReference reference = m_BufferStorage.GetUnusedBuffer( m_Type ) as ZipReference;
   try
   {
    if( !System.IO.Directory.Exists( strPath ) )
     System.IO.Directory.CreateDirectory( strPath );
    s      = new ZipInputStream( new System.IO.FileStream( strZipFile , System.IO.FileMode.Open , System.IO.FileAccess.Read ) );
    int      readLen;
    ZipEntry    theEntry;
    string     name;
    string     path;
    System.IO.FileStream fs;
    while( ( theEntry = s.GetNextEntry() ) != null )
    {
     if( theEntry.Size < 0 )
      continue;
     if( createSubDir )
     {
      name =  strPath + "\\" + theEntry.Name;
      if( theEntry.IsDirectory )
      {
       System.IO.Directory.CreateDirectory( name );
       continue;
      }
      path = System.IO.Path.GetDirectoryName( name );
      if( !System.IO.Directory.Exists( path ) )
       System.IO.Directory.CreateDirectory( path );
     }
     else
     {
      if( theEntry.IsDirectory )
       continue;
      name = strPath + "\\" + System.IO.Path.GetFileName( theEntry.Name );
     }
     fs = new System.IO.FileStream( name , System.IO.FileMode.OpenOrCreate , System.IO.FileAccess.Write );
     while( ( readLen = s.Read( reference.m_Buffer , 0 , m_BufferLen ) ) > 0 )
      fs.Write( reference.m_Buffer , 0 , readLen );
     fs.Flush();
     fs.Close();
    }
   }
   catch( Exception exp )
   {
    System.Console.WriteLine( exp.Message );
   }
   finally
   {
    reference.Used = false;
    try{ if( s != null ) s.Close(); }
    catch{}
   }
  }

  public static void UnzipFiles( string strZipFile , string strPath , bool createSubDir ,ZipEventHandler handler )
  {
   ZipInputStream   s = null;
   ZipReference reference = m_BufferStorage.GetUnusedBuffer( m_Type ) as ZipReference;
   try
   {
    if( !System.IO.Directory.Exists( strPath ) )
     System.IO.Directory.CreateDirectory( strPath );
    s      = new ZipInputStream( new System.IO.FileStream( strZipFile , System.IO.FileMode.Open , System.IO.FileAccess.Read ) );
    int      readLen;
    ZipEntry    theEntry;
    string     name;
    string     path;
    System.IO.FileStream fs;
    while( ( theEntry = s.GetNextEntry() ) != null )
    {
     if( theEntry.Size < 0 )
      continue;
     if( createSubDir )
     {
      name =  strPath + "\\" + theEntry.Name;
      if( theEntry.IsDirectory )
      {
       System.IO.Directory.CreateDirectory( name );
       continue;
      }
      path = System.IO.Path.GetDirectoryName( name );
      if( !System.IO.Directory.Exists( path ) )
       System.IO.Directory.CreateDirectory( path );
     }
     else
     {
      if( theEntry.IsDirectory )
       continue;
      name = strPath + "\\" + System.IO.Path.GetFileName( theEntry.Name );
     }
     fs = new System.IO.FileStream( name , System.IO.FileMode.OpenOrCreate , System.IO.FileAccess.Write );
     while( ( readLen = s.Read( reference.m_Buffer , 0 , m_BufferLen ) ) > 0 )
      fs.Write( reference.m_Buffer , 0 , readLen );
     fs.Flush();
     fs.Close();
     if( handler != null )
      handler( name , theEntry.CompressedSize , theEntry.Size , false );
    }
   }
   catch( Exception exp )
   {
    System.Console.WriteLine( exp.Message );
   }
   finally
   {
    reference.Used = false;
    try{ if( s != null ) s.Close(); }
    catch{}
   }
  }

  public static void ZipFiles( string[] files , string outFile , bool fullPath )
  {
   ZipOutputStream   s  = null;
   ZipReference reference = m_BufferStorage.GetUnusedBuffer( m_Type ) as ZipReference;
   int      readlen;
   try
   {
    string strPath   = System.IO.Path.GetDirectoryName( outFile );
    if( !System.IO.Directory.Exists( strPath ) )
     System.IO.Directory.CreateDirectory( strPath );

    s = new ZipOutputStream( new System.IO.FileStream( outFile , System.IO.FileMode.Create , System.IO.FileAccess.Write ) );
    System.IO.FileStream fs;
    string strName;
    foreach( string strFile in files )
    {
     try{ fs = new FileStream( strFile , System.IO.FileMode.Open , System.IO.FileAccess.Read );}
     catch{ continue; }
     if( fullPath )
      strName = strFile;
     else
      strName = System.IO.Path.GetFileName( strFile );
     ZipEntry entry = new ZipEntry( strName );
     s.PutNextEntry( entry );
     while( ( readlen = fs.Read( reference.m_Buffer , 0 , m_BufferLen ) ) > 0 )
      s.Write( reference.m_Buffer , 0 , readlen );
     fs.Close();
    }
   }
   catch( Exception exp )
   {
    System.Console.WriteLine( exp.Message );
   }
   finally
   {
    reference.Used = false;
    try{ if( s != null ) s.Close(); }
    catch{}
   }
  }

  public static void ZipFiles( string[] files , string outFile , bool fullPath , ZipEventHandler handler )
  {
   ZipOutputStream   s  = null;
   ZipReference reference = m_BufferStorage.GetUnusedBuffer( m_Type ) as ZipReference;
   try
   {
    string strPath   = System.IO.Path.GetDirectoryName( outFile );
    if( !System.IO.Directory.Exists( strPath ) )
     System.IO.Directory.CreateDirectory( strPath );

    s = new ZipOutputStream( new System.IO.FileStream( outFile , System.IO.FileMode.Create , System.IO.FileAccess.Write ) );
    System.IO.FileStream fs;
    string strName;
    int readlen;
    foreach( string strFile in files )
    {
     try{ fs = new FileStream( strFile , System.IO.FileMode.Open , System.IO.FileAccess.Read );}
     catch{ continue; }
     if( fullPath )
      strName = strFile;
     else
      strName = System.IO.Path.GetFileName( strFile );
     ZipEntry entry = new ZipEntry( strName );
     s.PutNextEntry( entry );
     while( ( readlen = fs.Read( reference.m_Buffer , 0 , m_BufferLen ) ) > 0 )
      s.Write( reference.m_Buffer , 0 , readlen );
     fs.Close();
     if( handler != null )
      handler( strFile , entry.CompressedSize , entry.Size , true );
    }
   }
   catch( Exception exp )
   {
    System.Console.WriteLine( exp.Message );
   }
   finally
   {
    reference.Used = false;
    try{ if( s != null ) s.Close(); }
    catch{}
   }
  }
 }
}

posted on 2004-08-17 23:14  kingdom  阅读(393)  评论(0)    收藏  举报

导航