ASP.NET 中有什么常用压缩文件夹的方式。请教下。
原来我程序里面用的是调用WINRAR方式压缩文件假的。但是有如下几个缺点。
1、占用CPU暴高一旦开始压缩文件夹。CUP一直是95%之上。机器让其他的事情什么都做不了
2、压缩大数据量时丢失进程。测试后发现压缩一个文件夹里面大概500个文件。大小300M 容易丢失进程。应为等待时间太长网页进程丢失(没找到什么好的解决办法)。
原来我程序里面用的是调用WINRAR方式压缩文件假的。但是有如下几个缺点。
1、占用CPU暴高一旦开始压缩文件夹。CUP一直是95%之上。机器让其他的事情什么都做不了
2、压缩大数据量时丢失进程。测试后发现压缩一个文件夹里面大概500个文件。大小300M 容易丢失进程。应为等待时间太长网页进程丢失(没找到什么好的解决办法)。
干在网上找了个压缩成ZIP格式的 370文件 477M CUP30%左右 时间1分钟以内
2个文件 1.87G CUP30% 时间3分钟以内
但是ZIP格式最大只能压缩2G文件夹 纠结
看来要找平衡 楼上的说AJAX 还是多线程来实现这个?
RAR格式太慢 CPU又高
ZIP格式到是快 但是太小
找平衡啊 纠结
http://zhangweiguo3984.cnblogs.com/articles/314329.html
http://zhangweiguo3984.cnblogs.com/articles/314333.html
class clsZip
{
public void CompressFile ( string sourceFile, string destinationFile )
{
// make sure the source file is there
if ( File.Exists ( sourceFile ) == false )
throw new FileNotFoundException ( ); // Create the streams and byte arrays needed
byte[] buffer = null;
FileStream sourceStream = null;
FileStream destinationStream = null;
GZipStream compressedStream = null; try
{
// Read the bytes from the source file into a byte array
sourceStream = new FileStream ( sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read ); // Read the source stream values into the buffer
buffer = new byte[sourceStream.Length];
int checkCounter = sourceStream.Read ( buffer, 0, buffer.Length ); if ( checkCounter != buffer.Length )
{
throw new ApplicationException ( );
} // Open the FileStream to write to
destinationStream = new FileStream ( destinationFile, FileMode.OpenOrCreate, FileAccess.Write ); // Create a compression stream pointing to the destiantion stream
compressedStream = new GZipStream ( destinationStream, CompressionMode.Compress, true ); // Now write the compressed data to the destination file
compressedStream.Write ( buffer, 0, buffer.Length );
}
catch ( ApplicationException ex )
{
MessageBox.Show ( ex.Message, "压缩文件时发生错误: ", MessageBoxButtons.OK, MessageBoxIcon.Error );
}
finally
{
// Make sure we allways close all streams
if ( sourceStream != null )
sourceStream.Close ( ); if ( compressedStream != null )
compressedStream.Close ( ); if ( destinationStream != null )
destinationStream.Close ( );
}
} public void DecompressFile ( string sourceFile, string destinationFile )
{
// make sure the source file is there
if ( File.Exists ( sourceFile ) == false )
throw new FileNotFoundException ( ); // Create the streams and byte arrays needed
FileStream sourceStream = null;
FileStream destinationStream = null;
GZipStream decompressedStream = null;
byte[] quartetBuffer = null; try
{
// Read in the compressed source stream
sourceStream = new FileStream ( sourceFile, FileMode.Open ); // Create a compression stream pointing to the destiantion stream
decompressedStream = new GZipStream ( sourceStream, CompressionMode.Decompress, true ); // Read the footer to determine the length of the destiantion file
quartetBuffer = new byte[4];
int position = (int)sourceStream.Length - 4;
sourceStream.Position = position;
sourceStream.Read ( quartetBuffer, 0, 4 );
sourceStream.Position = 0;
int checkLength = BitConverter.ToInt32 ( quartetBuffer, 0 ); byte[] buffer = new byte[checkLength + 100]; int offset = 0;
int total = 0; // Read the compressed data into the buffer
while ( true )
{
int bytesRead = decompressedStream.Read ( buffer, offset, 100 ); if ( bytesRead == 0 )
break; offset += bytesRead;
total += bytesRead;
} // Now write everything to the destination file
destinationStream = new FileStream ( destinationFile, FileMode.Create );
destinationStream.Write ( buffer, 0, total ); // and flush everyhting to clean out the buffer
destinationStream.Flush ( );
}
catch ( ApplicationException ex )
{
MessageBox.Show(ex.Message, "解压文件时发生错误: ", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
// Make sure we allways close all streams
if ( sourceStream != null )
sourceStream.Close ( ); if ( decompressedStream != null )
decompressedStream.Close ( ); if ( destinationStream != null )
destinationStream.Close ( );
} }
}