Design a command line splitter with C #

zhaozj2021-02-16  48

Design a command line splitter with C #

There are many beautiful file segmentation programs in sharing software, split a large file into several smaller files, saved, and then merged into the original file, often a must-have tool.

This article uses a Microsoft's Visual Studio .NET development environment C # language to design a file splitter, and run in a DOS command line, just as simple as using the Copy command. You can also understand the principle of file segmentation and the .NET framework read and write operation mode.

Create a new Visual C # item, select "Console Application", the project name is set to FILESPLITCON, the system automatically generates the class1.cs file, add the following code to the corresponding area of ​​Class1.cs:

Using system;

Using system.data;

Using system.io;

Namespace filesisplitcon

{

///

/// Class1 summary description.

///

Class class1

{

///

/// The main entry point for the application.

///

[Stathread]

Static void

Main

(String [] ARGS)

{

IF (args [0] == "s")

{

INT I;

FileStream fr = new filestream (Args [1], FileMode.Open, FileAccess.Read;

INT filesize = convert.Toint32 (fr.length) /convert.toint32 (ax);

Streamwriter fwlist = new streamwriter (args [1] "." "List", false;

For (i = 1; i <= convert.toint32 (args [2]); i )

{

Byte [] byteread = new byte [filesis];

Fr.Read (Byteread, 0, FileSize);

FILESTREAM FW = New FileStream (Args [1] "." I, filemode.createnew, fileaccess.write;

Fwlist.writeline (Args [1] "." i);

Foreach (Byte Bnext in Byteread)

fw.writebyte (BNEX);

Fw.close ();

}

IF (fr.length! = fr.position)

{

Byte [] byteread = new byte [convert.toint32 (fr.length) -filesis * (i-1)];

Fr.read (Byteread, 0, Convert.Toint32 (fr.length) -filesis * (i-1));

FILESTREAM FW = New FileStream (Args [1] "." I, filemode.createnew, fileaccess.write;

Fwlist.writeline (Args [1] "." i);

Foreach (Byte Bnext in Byteread)

fw.writebyte (BNEX);

Fw.close ();

}

Fr.close ();

Fwlist.flush (); fwlist.close ();

Console.WriteLine ("Document Split");

}

IF (args [0] == "m")

{

StreamReader FRLIST = New StreamReader (Args [1] "." "List");

FILESTREAM FW = New FileStream (Args [1], FileMode.Append, FileAccess.write;

String sline;

Sline = ferlist.readline ();

While (sline! = NULL)

{

FILESTREAM fr = new filestream (sline, filemode.open, fileaccess.read);

Byte [] byteread = new byte [fr.length];

Fr.read (Byteread, 0, Convert.Toint32 (Fr.Length);

Foreach (Byte Bnext in Byteread)

fw.writebyte (BNEX);

Fr.close ();

Sline = ferlist.readline ();

}

FRLIST.CLOSE ();

Fw.close ();

Console.WriteLine ("File Merge End");

}

}

}

}

After compiling, the executable file is generated under bin / debug, with a size of only 6.5kB, copying this executor to the path that can be found, such as C: / WinNT (Windows2000 system), making it a system command.

How to use under the command prompt window (assuming that the compiled executable is filesplitcon.exe):

Split file:

FILESPLITCON S is divided into several files to split file names

For example, FILESPLITCON S W2KSP3.EXE 4 (dividing w2ksp3.exe into 4 files)

Merger files:

FILESPLITCON M merge file name

The code is very simple, using the StreamWriter and the StreamReader object, the loop reads the file in a BYTE byte, the first parameter args [0] = "s" means executing the division file code, args [0] = "m" means execution Merger file code.

Description:

At the time of segmentation, generate each segmentation file in the same directory as the division file, the file name is added to the original file name. 1, .2, .3, .....; simultaneously generate a list file .list, record generation The file name of each divided file;

For example, FILESPLITCON S W2KSP3.EXE 4;

W2ksp3.exe.1

W2ksp3.exe.2

W2ksp3.exe.3

W2ksp3.exe.4

W2ksp3.exe.list

The content of the w2ksp3.exe.list file is:

W2ksp3.exe.1

W2ksp3.exe.2

W2ksp3.exe.3

W2ksp3.exe.4

When combined,

W2ksp3.exe.1

W2ksp3.exe.2

W2ksp3.exe.3

W2ksp3.exe.4

W2ksp3.exe.list

5 files copy to any directory, execute the command:

FILESPLITCON M w2ksp3.exe

Note: If a file cannot split several files, the processing of the above programs is to write the remaining bytes to a new file, so for the above example, if w2ksp3.exe can't just divide 4 files, Then, 5 files will be generated so that processing should be closer to the actual situation. The above program is debugged in the Windows2000, the Visual Studio .NET development environment, because the command line mode is used, the running speed is very fast, and a 124M file is divided into 4 files that can't feel waiting.

转载请注明原文地址:https://www.9cbs.com/read-23309.html

New Post(0)