文件流转换

1
2
3
4
5
6
7
8
9
string BasePath = AppDomain.CurrentDomain.BaseDirectory;
string path = @"d:\demo\测试.zip";
byte[] buff = FileBinaryConvertHelper.File2Bytes(path);

string JarContent = Convert.ToBase64String(buff);
File.WriteAllText(@"d:\demo\测试.txt", JarContent);

string data = File.ReadAllText(@"d:\demo\测试.txt", Encoding.Default);
FileBinaryConvertHelper.Bytes2File(Convert.FromBase64String(data), BasePath + @"\Files\测试.zip");

FileBinaryConvertHelper

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class FileBinaryConvertHelper
{
/// <summary>
/// 将文件转换为byte数组
/// </summary>
/// <param name="path">文件地址</param>
/// <returns>转换后的byte数组</returns>
public static byte[] File2Bytes(string path)
{
if (!System.IO.File.Exists(path))
{
return new byte[0];
}

FileInfo fi = new FileInfo(path);
byte[] buff = new byte[fi.Length];

FileStream fs = fi.OpenRead();
fs.Read(buff, 0, Convert.ToInt32(fs.Length));
fs.Close();

return buff;
}

/// <summary>
/// 将byte数组转换为文件并保存到指定地址
/// </summary>
/// <param name="buff">byte数组</param>
/// <param name="savepath">保存地址</param>
public static void Bytes2File(byte[] buff, string savepath)
{
if (System.IO.File.Exists(savepath))
{
System.IO.File.Delete(savepath);
}

FileStream fs = new FileStream(savepath, FileMode.CreateNew);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(buff, 0, buff.Length);
bw.Close();
fs.Close();
}
}

zip 压缩

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public void ZipFile(string strFile, string strZip)
{
if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar)
strFile += Path.DirectorySeparatorChar;
ZipOutputStream s = new ZipOutputStream(File.Create(strZip));
s.SetLevel(6); // 0 - store only to 9 - means best compression
zip(strFile, s, strFile);
s.Finish();
s.Close();
}

private void zip(string strFile, ZipOutputStream s, string staticFile)
{
if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar) strFile
+= Path.DirectorySeparatorChar;
Crc32 crc = new Crc32();
string[] filenames = Directory.GetFileSystemEntries(strFile);
foreach (string file in filenames)
{

if (Directory.Exists(file))
{
zip(file, s, staticFile);
}

else // 否则直接压缩文件
{
//打开压缩文件
FileStream fs = File.OpenRead(file);

byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
string tempfile = file.Substring(staticFile.LastIndexOf("\\") + 1);
ZipEntry entry = new ZipEntry(tempfile);

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);
}
}
}

zip 解压

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
public static Dictionary<string, string> UnZip(string zipFilePath, string unZipDir)
{
Dictionary<string, string> files = new Dictionary<string, string>();
if (zipFilePath == string.Empty)
{
throw new Exception("压缩文件不能为空!");
}
if (!File.Exists(BasePath + zipFilePath))
{
throw new FileNotFoundException("压缩文件不存在!");
}
//解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹
if (unZipDir == string.Empty)
unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath),
Path.GetFileNameWithoutExtension(zipFilePath));
if (!unZipDir.EndsWith("/"))
unZipDir += "/";
if (!Directory.Exists(BasePath + unZipDir))
Directory.CreateDirectory(BasePath + unZipDir);

try
{
using (var s = new ZipInputStream(File.OpenRead(BasePath + zipFilePath)))
{

ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = Path.GetDirectoryName(theEntry.Name);
string fileName = Path.GetFileName(theEntry.Name);
if (!string.IsNullOrEmpty(directoryName))
{
Directory.CreateDirectory(BasePath + unZipDir + directoryName);
}
if (directoryName != null && !directoryName.EndsWith("/"))
{
}
if (fileName != String.Empty)
{
using (FileStream streamWriter = File.Create(BasePath + unZipDir + theEntry.Name))
{
int size;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
files.Add(fileName, unZipDir + theEntry.Name);
}

}
}
}
}
catch (Exception ex)
{
throw new Exception("压缩文件解压失败!\n" + ex);
}
return files;
}

ByteToFile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private static string ByteToFile(string fileName, byte[] pReadByte)
{
FileStream pFileStream = null;
try
{
pFileStream = new FileStream(BasePath + fileName, FileMode.OpenOrCreate);
pFileStream.Write(pReadByte, 0, pReadByte.Length);
}
catch (Exception ex)
{
return ex.Message;
}
finally
{
if (pFileStream != null)
pFileStream.Close();
}
return fileName;
}