📄 fileoperation.cs
字号:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
namespace Scanner
{
public class FileOperation
{
public string Read(string path)
{
int LineNo = 1;
StringBuilder str = new StringBuilder();
using (StreamReader sr = new StreamReader(path, System.Text.Encoding.Default))
{
try
{
while (sr.Peek() != -1)
{
str.Append("第");
string content = LineNo.ToString();
str.Append(content.PadLeft(4,'0'));
str.Append("行: ");
str.Append(sr.ReadLine());
str.Append("\r\n");
LineNo++;
}
}
catch (IOException ex)
{
throw ex;
}
}
return str.ToString();
}
private string Filter(string content)
{
string str = content.Trim();
int indexd = str.IndexOf(@"//");
int indexs = str.IndexOf(@"//{{");
int indexe = str.IndexOf(@"//}}");
if (indexd != -1 && indexe == -1 && indexs == -1)
{
str = str.Remove(indexd);
}
return str.Trim();
}
public int Write(string str, string newpath)
{
int size = 0;
try
{
if (File.Exists(newpath))
{
File.Delete(newpath);
}
using (FileStream fs = File.Create(newpath))
{
Byte[] info = new UTF8Encoding(true).GetBytes(str);
size = info.Length;
fs.Write(info, 0, size);
}
}
catch (Exception ex)
{
throw ex;
}
return size;
}
public int compress(string path,string newpath)
{
int size=0;
StringBuilder str = new StringBuilder();
using (StreamReader sr = new StreamReader(path, System.Text.Encoding.Default))
{
try
{
while (sr.Peek() != -1)
{
str.Append(Filter(sr.ReadLine()));
int indexs = str.ToString().IndexOf(@"/*");
int indexe = str.ToString().IndexOf(@"*/");
if (indexs != -1 && indexe != -1)
{
str.Remove(indexs, indexe - indexs + 2);
}
size= Write(str.ToString(), newpath);
}
}
catch (IOException ex)
{
throw ex;
}
}
return size;
}
public void SingleHighCompress(string path,string compresspath)
{
try
{
Crc32 crc = new Crc32();
ZipOutputStream s = new ZipOutputStream(File.Create(compresspath+"\\compress.zip"));
s.SetLevel(6);
FileStream fs = File.OpenRead(path);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(path.Remove(0, path.LastIndexOf("//") + 1));
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
s.Finish();
s.Close();
}
catch (Exception e)
{
throw e;
}
}
public int MultiHighCompress(string path, string compresspath)
{
int size = 0;
string newpath = compresspath + "\\Compress.zip";
string[] filenames = Directory.GetFiles(path);
if (filenames.Length > 0)
{
byte[] buffer = new byte[1024];
using (ZipOutputStream zipoutput = new ZipOutputStream(File.Create(newpath)))
{
zipoutput.SetLevel(9);
foreach (string filename in filenames)
{
ZipEntry entry = new ZipEntry(filename.Remove(0,filename.LastIndexOf("//")+1));
zipoutput.PutNextEntry(entry);
using (FileStream fs = File.OpenRead(filename))
{
StreamUtils.Copy(fs, zipoutput, buffer);
}
}
}
using (FileStream fs = File.OpenRead(newpath))
{
size = (int)fs.Length;
}
}
return size;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -