拜托我不希望用ZIP的,我希望是自己写的一个文本压缩方法!
解决方案 »
- C#怎么实现按月循环
- windows 窗体如何改变风格
- 关于C#选择一个图形做操作的问题?
- 通过TCPIP协议 从数据采集器 获得温度,湿度,电压,电流,这些变量 怎么实现 百分求教
- 如何根据控件的所有属性,生成一个控件的相同拷贝??在线等待,高手指点!!!
- 数据转出到Excel,汉字为何是乱码?[内有代码]
- 关于jmail ,为什么会弹出“the message war undeliverable,all servers failed to receive the message"
- 如何导出sql数据到EXCL
- 请问在Richtextbox中,选中文字的字体不同时该如何处理
- 在C# Windows Form中如何单击左边的treeview,右边的内容变化
- 在FlexGrid中怎么实现焦点冻结在某一个单元格?
- 为什么程序运行出现异常???
/// <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
ComponentOne那里都有的,下一个好了!
在IBM DEVELOPERWORKS中有很多关于XML压缩的讨论,去看看吧