[C#]把文件分割又重写了一遍

最早的是易语言写的,上学期用 VB6 重写了一遍,现在就用 C# 写,写了个 dll,又搞了 CMD 和 UI 两个版本,呵呵 。贴个关键代码吧:


using System;
using System.Text;
using System.IO;

namespace FS
{
    
public class FileSplitter
    
{
        
private enum MSG 
        
{
            
FILE_NOT_EXIST          1,
            
DIR_NOT_EXIST           2,
            
BLOCKSIZE_ERROR         3,
            
BLOCKSIZE_TOO_LARGE     4,
            
BLOCKSIZE_TOO_SMALL     5,
            
CAN_NOT_OPEN_FILE       6,
            
CAN_NOT_CREATE_FILE     7,
            
CAN_NOT_CREATE_CMDFILE  8
        
};
        
private string m_srcPath;
        
private string m_destDir;
        
private long m_nBlockSize 0;
        
private MSG m_byError 0;

        
public FileSplitter(string srcPath)
        {
            
m_srcPath srcPath;
            
m_destDir Path.GetDirectoryName(m_srcPath);
        }

        
public FileSplitter(string srcPathstring destDir)
        {
            
m_srcPath srcPath;
            
m_destDir destDir;
        }

        
public FileSplitter(string srcPathlong nBlockSize)
        {
            
m_srcPath srcPath;
            
m_destDir Path.GetDirectoryName(m_srcPath);
            
m_nBlockSize nBlockSize;
        }

        
public FileSplitter(string srcPathstring destDirlong nBlockSize)
        {
            
m_srcPath srcPath;
            
m_destDir destDir;
            
m_nBlockSize nBlockSize;
        }

        
public bool SplitFile()
        {
            
if (!File.Exists(m_srcPath))
            {
                
m_byError MSG.FILE_NOT_EXIST;
                
return false;
            }

            
if (!Directory.Exists(m_destDir))
            {
                
m_byError MSG.DIR_NOT_EXIST;
                
return false;
            }

            
FileInfo fi new FileInfo(m_srcPath);

            
if (m_nBlockSize == 0)
            {
                
m_nBlockSize fi.Length;
            }

            
if (m_nBlockSize <= 0)
            {
                
m_byError MSG.BLOCKSIZE_ERROR;
                
return false;
            }

            
if (m_nBlockSize >= fi.Length)
            {
                
m_byError MSG.BLOCKSIZE_TOO_LARGE;
                
return false;
            }

            
if (fi.Length m_nBlockSize 999)
            {
                
m_byError MSG.BLOCKSIZE_TOO_SMALL;
                
return false;
            }

            
FileStream fs_in;
            
try
            
{
                
fs_in new FileStream(m_srcPathFileMode.Open);
            }
            
catch
            
{
                
m_byError MSG.CAN_NOT_OPEN_FILE;
                
return false;
            }

            
long file_rest fi.Length ;
            
string temp "COPY /B ";
            
int index 1;
            
byte[] bytes new byte[1024];
            

            
while (file_rest 0)
            {
                
string index_ext "00" index.ToString();
                
FileStream fs_out;
                
try
                
{
                    
fs_out new FileStream(m_destDir "//" fi.Name "." index_ext.Substring(index_ext.Length 3), FileMode.Create);
                }
                
catch 
                
{
                    
m_byError MSG.CAN_NOT_CREATE_FILE;
                    
return false;
                }
                
temp += "/"" fi.Name "." index_ext.Substring(index_ext.Length 3) + "/" + ";
                
long block_rest;
                
                
if(file_rest m_nBlockSize )
                {
                    
block_rest m_nBlockSize;
                }
                
else
                
{
                    
block_rest file_rest;
                }

                
file_rest -= block_rest;

                
while (block_rest 0)
                {
                    
int count;

                    
if (block_rest 1024)
                    {
                        
count 1024;
                    }
                    
else
                    
{
                        
count = (int)block_rest;
                    }

                    
block_rest -=count;

                    
fs_in.Read(bytes0count);
                    
fs_out.Write(bytes0count);

                }

                
fs_out.Close();
                
index++;
            }

            
temp temp.Substring(0temp.Length 2) + "/"" fi.Name "/"";

            
StreamWriter sw_cmd;
            
try
            
{
                
sw_cmd new StreamWriter(m_destDir "//join.cmd"falseEncoding.Default);
            }
            
catch
            
{
                
m_byError MSG.CAN_NOT_CREATE_CMDFILE;
                
return false;
            }
            
sw_cmd.WriteLine("@echo off");
            
sw_cmd.WriteLine("cls");
            
sw_cmd.WriteLine("echo 溪流文件分割 v6.0");
            
sw_cmd.WriteLine("echo Copyright(C) 1998-2007 溪流软件工作室");
            
sw_cmd.WriteLine("echo.");
            
sw_cmd.WriteLine("");
            
sw_cmd.WriteLine(":CheckFile");
            
sw_cmd.WriteLine("echo 正在检测文件...");
            
sw_cmd.WriteLine("echo.");
            
sw_cmd.WriteLine("IF EXIST /"" fi.Name "/" GOTO FileAlreadyExist");
            
sw_cmd.WriteLine("GOTO JoinFiles");
            
sw_cmd.WriteLine("");
            
sw_cmd.WriteLine(":FileAlreadyExist");
            
sw_cmd.WriteLine("echo “" fi.Name "”已经存在,进行合并将覆盖文件。");
            
sw_cmd.WriteLine("echo 要继续进行合并的话,请将“FileName”改名、");
            
sw_cmd.WriteLine("echo 转移或删除。");
            
sw_cmd.WriteLine("GOTO End");
            
sw_cmd.WriteLine("");
            
sw_cmd.WriteLine(":JoinFiles");
            
sw_cmd.WriteLine("echo.");
            
sw_cmd.WriteLine("echo 正在合并文件...");
            
sw_cmd.WriteLine(temp);
            
sw_cmd.WriteLine("echo 成功合并了文件“" fi.Name "”。");
            
sw_cmd.WriteLine("GOTO End");
            
sw_cmd.WriteLine("");
            
sw_cmd.WriteLine(":End");
            
sw_cmd.WriteLine("echo.");
            
sw_cmd.WriteLine("echo.");
            
sw_cmd.WriteLine("pause");
            
sw_cmd.WriteLine("echo on");
            
sw_cmd.WriteLine("exit");
            
sw_cmd.Close();

            
return true;
        }

        
public string GetErrorInfo()
        {
            
switch (m_byError)
            { 
                
case MSG.FILE_NOT_EXIST:
                    
return "文件不存在。";
                
case MSG.DIR_NOT_EXIST:
                    
return "目录不存在。";
                
case MSG.BLOCKSIZE_ERROR :
                    
return "块大小错误。";
                
case MSG.BLOCKSIZE_TOO_LARGE :
                    
return "块大小过大,不需要分割。";
                
case MSG.BLOCKSIZE_TOO_SMALL :
                    
return "块大小过小,分割后将产生多于 1000 个文件,拒绝分割。";
                
case MSG.CAN_NOT_OPEN_FILE :
                    
return "打开文件失败。";
                
case MSG.CAN_NOT_CREATE_FILE :
                    
return "创建文件失败。";
                
case MSG.CAN_NOT_CREATE_CMDFILE:
                    
return "创建命令文件失败。";
                
default:
                    
return "";
            }
        }
    }
}

 

(原发表于 CSDN:https://blog.csdn.net/cnStreamlet/article/details/1655902)

posted on 2007-06-17 23:01  溪流  阅读(41)  评论(0编辑  收藏  举报