怎样对一个备份的数据库文件压缩? 怎样对一个备份的数据库文件压缩?ASP.NET/C# 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 需安装ComponentOne#region Class Cls_ZIP/// <summary>/// This Class is for zip folder/// </summary>public class Cls_ZIP{ public const string DIR_COMP = @"\compressed"; public const string DIR_EXP = @"\expanded"; public string FileZip(string foldername) { // get application directory string currentpath = Application.ExecutablePath.ToString(); int i = currentpath.IndexOf(@"\bin\"); if (i > 0) currentpath = currentpath.Substring(0, i); // create directory for compressed files if (Directory.Exists(currentpath + DIR_COMP)) Directory.Delete(currentpath + DIR_COMP, true); Directory.CreateDirectory(currentpath + DIR_COMP); // prepare to collect compression statistics long count = 0; long size = 0; long sizeCompressed = 0; long ticks = DateTime.Now.Ticks; // compress all files in application dir into compressed dir string[] files = Directory.GetFiles(foldername); foreach (string srcFile in files) { // compress file string dstFile = currentpath + DIR_COMP + "\\" + Path.GetFileName(srcFile) + ".cmp"; CompressFile(dstFile, srcFile); // update stats count++; size += new FileInfo(srcFile).Length; sizeCompressed += new FileInfo(dstFile).Length; } // show stats string msg = string.Format( "Compressed {0} files in {1} ms.\r\n" + "Original size: {2:#,###}\r\n" + "Compressed size: {3:#,###} ({4:0.00}% of original)", count, (DateTime.Now.Ticks - ticks) / TimeSpan.TicksPerMillisecond, size, sizeCompressed, (sizeCompressed / (double)size) * 100.0); return msg; } public string FileUnzip(string foldername) { // get application directory string path = Application.ExecutablePath; int i = path.IndexOf(@"\bin\"); if (i > 0) path = path.Substring(0, i); // create directory for expanded files if (Directory.Exists(path + DIR_EXP)) Directory.Delete(path + DIR_EXP, true); Directory.CreateDirectory(path + DIR_EXP); // prepare to collect compression statistics long count = 0; long size = 0; long sizeExpanded = 0; long ticks = DateTime.Now.Ticks; // expand all files in "compressed" dir to "expanded" dir string[] files = Directory.GetFiles(path + DIR_COMP); foreach (string srcFile in files) { // expand file string dstFile = path + DIR_EXP + "\\" + Path.GetFileName(srcFile); dstFile = dstFile.Replace(".cmp", ""); ExpandFile(dstFile, srcFile); // update stats count++; size += new FileInfo(srcFile).Length; sizeExpanded += new FileInfo(dstFile).Length; } // show stats string msg = string.Format( "Expanded {0} files in {1} ms.\r\n" + "Original size: {2:#,###}\r\n" + "Expanded size: {3:#,###} ({4:0.00} x size of compressed)", count, (DateTime.Now.Ticks - ticks) / TimeSpan.TicksPerMillisecond, size, sizeExpanded, sizeExpanded / (double)size); return msg; } private static bool CompressFile(string dstFile, string srcFile) { // prepare to compress file bool retval = true; FileStream srcStream = null; FileStream dstStream = null; try { // open the files srcStream = new FileStream(srcFile, FileMode.Open, FileAccess.Read); dstStream = new FileStream(dstFile, FileMode.Create, FileAccess.Write); // open compressor stream on destination file C1ZStreamWriter sw = new C1ZStreamWriter(dstStream); // copy source into compressor stream StreamCopy(sw, srcStream); } catch // exception? tell caller we failed { retval = false; } finally // always close our streams { if (srcStream != null) srcStream.Close(); if (dstStream != null) dstStream.Close(); } // done return retval; } private static bool ExpandFile(string dstFile, string srcFile) { // prepare to expand file bool retval = true; FileStream srcStream = null; FileStream dstStream = null; try { // open the files srcStream = new FileStream(srcFile, FileMode.Open, FileAccess.Read); dstStream = new FileStream(dstFile, FileMode.Create, FileAccess.Write); // open expander stream on compressed source C1ZStreamReader sr = new C1ZStreamReader(srcStream); // copy expander stream into destination file StreamCopy(dstStream, sr); } catch // exception? tell caller we failed { retval = false; } finally // always close our streams { if (srcStream != null) srcStream.Close(); if (dstStream != null) dstStream.Close(); } // done return retval; } private static void StreamCopy(Stream dstStream, Stream srcStream) { byte[] buffer = new byte[32768]; int read; while ((read = srcStream.Read(buffer, 0, buffer.Length)) != 0) dstStream.Write(buffer, 0, read); dstStream.Flush(); }}#endregion 如何在一个asp.net项目中连接两个sqlserver数据库 求一个方法 日期格式转换问题+手写代码问题哦 菜鸟来了,高手请进,50分求助! 最后7天,最后一个问题,完了就上学,4年后见 web access failed--初学者提问! 在asp.net中,能否关闭当前页面后刷新另外一个页面,急急!!!!! 请问怎把文本框里值,插入到select这个HTML控件项当中? FileUpload控件的问题 如何给一个Label.Text属性付int值? 菜鸟提问:??在把SQL server改用access时出现的问题 怎样取得上网方式?
/// <summary>
/// This Class is for zip folder
/// </summary>
public class Cls_ZIP
{
public const string DIR_COMP = @"\compressed";
public const string DIR_EXP = @"\expanded"; public string FileZip(string foldername)
{
// get application directory
string currentpath = Application.ExecutablePath.ToString();
int i = currentpath.IndexOf(@"\bin\");
if (i > 0) currentpath = currentpath.Substring(0, i); // create directory for compressed files
if (Directory.Exists(currentpath + DIR_COMP))
Directory.Delete(currentpath + DIR_COMP, true);
Directory.CreateDirectory(currentpath + DIR_COMP); // prepare to collect compression statistics
long count = 0;
long size = 0;
long sizeCompressed = 0;
long ticks = DateTime.Now.Ticks; // compress all files in application dir into compressed dir
string[] files = Directory.GetFiles(foldername);
foreach (string srcFile in files)
{
// compress file
string dstFile = currentpath + DIR_COMP + "\\" + Path.GetFileName(srcFile) + ".cmp";
CompressFile(dstFile, srcFile); // update stats
count++;
size += new FileInfo(srcFile).Length;
sizeCompressed += new FileInfo(dstFile).Length;
} // show stats
string msg = string.Format(
"Compressed {0} files in {1} ms.\r\n" +
"Original size: {2:#,###}\r\n" +
"Compressed size: {3:#,###} ({4:0.00}% of original)",
count,
(DateTime.Now.Ticks - ticks) / TimeSpan.TicksPerMillisecond,
size, sizeCompressed,
(sizeCompressed / (double)size) * 100.0);
return msg;
} public string FileUnzip(string foldername)
{
// get application directory
string path = Application.ExecutablePath;
int i = path.IndexOf(@"\bin\");
if (i > 0) path = path.Substring(0, i); // create directory for expanded files
if (Directory.Exists(path + DIR_EXP))
Directory.Delete(path + DIR_EXP, true);
Directory.CreateDirectory(path + DIR_EXP); // prepare to collect compression statistics
long count = 0;
long size = 0;
long sizeExpanded = 0;
long ticks = DateTime.Now.Ticks; // expand all files in "compressed" dir to "expanded" dir
string[] files = Directory.GetFiles(path + DIR_COMP);
foreach (string srcFile in files)
{
// expand file
string dstFile = path + DIR_EXP + "\\" + Path.GetFileName(srcFile);
dstFile = dstFile.Replace(".cmp", "");
ExpandFile(dstFile, srcFile); // update stats
count++;
size += new FileInfo(srcFile).Length;
sizeExpanded += new FileInfo(dstFile).Length;
} // show stats
string msg = string.Format(
"Expanded {0} files in {1} ms.\r\n" +
"Original size: {2:#,###}\r\n" +
"Expanded size: {3:#,###} ({4:0.00} x size of compressed)",
count,
(DateTime.Now.Ticks - ticks) / TimeSpan.TicksPerMillisecond,
size, sizeExpanded,
sizeExpanded / (double)size);
return msg;
}
private static bool CompressFile(string dstFile, string srcFile)
{
// prepare to compress file
bool retval = true;
FileStream srcStream = null;
FileStream dstStream = null;
try
{
// open the files
srcStream = new FileStream(srcFile, FileMode.Open, FileAccess.Read);
dstStream = new FileStream(dstFile, FileMode.Create, FileAccess.Write); // open compressor stream on destination file
C1ZStreamWriter sw = new C1ZStreamWriter(dstStream); // copy source into compressor stream
StreamCopy(sw, srcStream);
}
catch // exception? tell caller we failed
{
retval = false;
}
finally // always close our streams
{
if (srcStream != null) srcStream.Close();
if (dstStream != null) dstStream.Close();
} // done
return retval;
}
private static bool ExpandFile(string dstFile, string srcFile)
{
// prepare to expand file
bool retval = true;
FileStream srcStream = null;
FileStream dstStream = null;
try
{
// open the files
srcStream = new FileStream(srcFile, FileMode.Open, FileAccess.Read);
dstStream = new FileStream(dstFile, FileMode.Create, FileAccess.Write); // open expander stream on compressed source
C1ZStreamReader sr = new C1ZStreamReader(srcStream); // copy expander stream into destination file
StreamCopy(dstStream, sr);
}
catch // exception? tell caller we failed
{
retval = false;
}
finally // always close our streams
{
if (srcStream != null) srcStream.Close();
if (dstStream != null) dstStream.Close();
} // done
return retval;
}
private static void StreamCopy(Stream dstStream, Stream srcStream)
{
byte[] buffer = new byte[32768];
int read;
while ((read = srcStream.Read(buffer, 0, buffer.Length)) != 0)
dstStream.Write(buffer, 0, read);
dstStream.Flush();
}
}
#endregion