2007-06-17 23:01:00
最早的是易语言写的,上学期用 VB6 重写了一遍,现在就用 C# 写,写了个 dll,又搞了 CMD 和 UI 两个版本,呵呵 。贴个关键代码吧:
1using System;
2using System.Text;
3using System.IO;
4
5namespace FS
6{
7 public class FileSplitter
8 {
9 private enum MSG
10 {
11 FILE_NOT_EXIST = 1,
12 DIR_NOT_EXIST = 2,
13 BLOCKSIZE_ERROR = 3,
14 BLOCKSIZE_TOO_LARGE = 4,
15 BLOCKSIZE_TOO_SMALL = 5,
16 CAN_NOT_OPEN_FILE = 6,
17 CAN_NOT_CREATE_FILE = 7,
18 CAN_NOT_CREATE_CMDFILE = 8
19 };
20 private string m_srcPath;
21 private string m_destDir;
22 private long m_nBlockSize = 0;
23 private MSG m_byError = 0;
24
25 public FileSplitter(string srcPath)
26 {
27 m_srcPath = srcPath;
28 m_destDir = Path.GetDirectoryName(m_srcPath);
29 }
30
31 public FileSplitter(string srcPath, string destDir)
32 {
33 m_srcPath = srcPath;
34 m_destDir = destDir;
35 }
36
37 public FileSplitter(string srcPath, long nBlockSize)
38 {
39 m_srcPath = srcPath;
40 m_destDir = Path.GetDirectoryName(m_srcPath);
41 m_nBlockSize = nBlockSize;
42 }
43
44 public FileSplitter(string srcPath, string destDir, long nBlockSize)
45 {
46 m_srcPath = srcPath;
47 m_destDir = destDir;
48 m_nBlockSize = nBlockSize;
49 }
50
51 public bool SplitFile()
52 {
53 if (!File.Exists(m_srcPath))
54 {
55 m_byError = MSG.FILE_NOT_EXIST;
56 return false;
57 }
58
59 if (!Directory.Exists(m_destDir))
60 {
61 m_byError = MSG.DIR_NOT_EXIST;
62 return false;
63 }
64
65 FileInfo fi = new FileInfo(m_srcPath);
66
67 if (m_nBlockSize == 0)
68 {
69 m_nBlockSize = fi.Length;
70 }
71
72 if (m_nBlockSize <= 0)
73 {
74 m_byError = MSG.BLOCKSIZE_ERROR;
75 return false;
76 }
77
78 if (m_nBlockSize >= fi.Length)
79 {
80 m_byError = MSG.BLOCKSIZE_TOO_LARGE;
81 return false;
82 }
83
84 if (fi.Length / m_nBlockSize > 999)
85 {
86 m_byError = MSG.BLOCKSIZE_TOO_SMALL;
87 return false;
88 }
89
90 FileStream fs_in;
91 try
92 {
93 fs_in = new FileStream(m_srcPath, FileMode.Open);
94 }
95 catch
96 {
97 m_byError = MSG.CAN_NOT_OPEN_FILE;
98 return false;
99 }
100
101 long file_rest = fi.Length ;
102 string temp = "COPY /B ";
103 int index = 1;
104 byte[] bytes = new byte[1024];
105
106
107 while (file_rest > 0)
108 {
109 string index_ext = "00" + index.ToString();
110 FileStream fs_out;
111 try
112 {
113 fs_out = new FileStream(m_destDir + "//" + fi.Name + "." + index_ext.Substring(index_ext.Length - 3), FileMode.Create);
114 }
115 catch
116 {
117 m_byError = MSG.CAN_NOT_CREATE_FILE;
118 return false;
119 }
120 temp += "/"" + fi.Name + "." + index_ext.Substring(index_ext.Length - 3) + "/" + ";
121 long block_rest;
122
123 if(file_rest > m_nBlockSize )
124 {
125 block_rest = m_nBlockSize;
126 }
127 else
128 {
129 block_rest = file_rest;
130 }
131
132 file_rest -= block_rest;
133
134 while (block_rest > 0)
135 {
136 int count;
137
138 if (block_rest > 1024)
139 {
140 count = 1024;
141 }
142 else
143 {
144 count = (int)block_rest;
145 }
146
147 block_rest -=count;
148
149 fs_in.Read(bytes, 0, count);
150 fs_out.Write(bytes, 0, count);
151
152 }
153
154 fs_out.Close();
155 index++;
156 }
157
158 temp = temp.Substring(0, temp.Length - 2) + "/"" + fi.Name + "/"";
159
160 StreamWriter sw_cmd;
161 try
162 {
163 sw_cmd = new StreamWriter(m_destDir + "//join.cmd", false, Encoding.Default);
164 }
165 catch
166 {
167 m_byError = MSG.CAN_NOT_CREATE_CMDFILE;
168 return false;
169 }
170 sw_cmd.WriteLine("@echo off");
171 sw_cmd.WriteLine("cls");
172 sw_cmd.WriteLine("echo 溪流文件分割 v6.0");
173 sw_cmd.WriteLine("echo Copyright(C) 1998-2007 溪流软件工作室");
174 sw_cmd.WriteLine("echo.");
175 sw_cmd.WriteLine("");
176 sw_cmd.WriteLine(":CheckFile");
177 sw_cmd.WriteLine("echo 正在检测文件...");
178 sw_cmd.WriteLine("echo.");
179 sw_cmd.WriteLine("IF EXIST /"" + fi.Name + "/" GOTO FileAlreadyExist");
180 sw_cmd.WriteLine("GOTO JoinFiles");
181 sw_cmd.WriteLine("");
182 sw_cmd.WriteLine(":FileAlreadyExist");
183 sw_cmd.WriteLine("echo “" + fi.Name + "”已经存在,进行合并将覆盖文件。");
184 sw_cmd.WriteLine("echo 要继续进行合并的话,请将“FileName”改名、");
185 sw_cmd.WriteLine("echo 转移或删除。");
186 sw_cmd.WriteLine("GOTO End");
187 sw_cmd.WriteLine("");
188 sw_cmd.WriteLine(":JoinFiles");
189 sw_cmd.WriteLine("echo.");
190 sw_cmd.WriteLine("echo 正在合并文件...");
191 sw_cmd.WriteLine(temp);
192 sw_cmd.WriteLine("echo 成功合并了文件“" + fi.Name + "”。");
193 sw_cmd.WriteLine("GOTO End");
194 sw_cmd.WriteLine("");
195 sw_cmd.WriteLine(":End");
196 sw_cmd.WriteLine("echo.");
197 sw_cmd.WriteLine("echo.");
198 sw_cmd.WriteLine("pause");
199 sw_cmd.WriteLine("echo on");
200 sw_cmd.WriteLine("exit");
201 sw_cmd.Close();
202
203 return true;
204 }
205
206 public string GetErrorInfo()
207 {
208 switch (m_byError)
209 {
210 case MSG.FILE_NOT_EXIST:
211 return "文件不存在。";
212 case MSG.DIR_NOT_EXIST:
213 return "目录不存在。";
214 case MSG.BLOCKSIZE_ERROR :
215 return "块大小错误。";
216 case MSG.BLOCKSIZE_TOO_LARGE :
217 return "块大小过大,不需要分割。";
218 case MSG.BLOCKSIZE_TOO_SMALL :
219 return "块大小过小,分割后将产生多于 1000 个文件,拒绝分割。";
220 case MSG.CAN_NOT_OPEN_FILE :
221 return "打开文件失败。";
222 case MSG.CAN_NOT_CREATE_FILE :
223 return "创建文件失败。";
224 case MSG.CAN_NOT_CREATE_CMDFILE:
225 return "创建命令文件失败。";
226 default:
227 return "";
228 }
229 }
230 }
231}
首发:https://blog.csdn.net/cnStreamlet/article/details/1655902