Second-binary dual-channel technology implementation file segmentation (2)

xiaoxiao2021-03-06  55

(2) Add a module module1, which contain various algorithms specific implementation process, double-click the module to write the following code:?

TYPE? FileSECTION?

Bytes ()? As? Byte?

END? TYPE? 'Defines the actual memory array?

TYPE? SECTIONEDFILE?

FILES ()? As? Filesection?

END? TYPE? 'Defines an auxiliary memory array for extended use?

TYPE? FILEINFO?

OrigProjsize? As? Long? 'File size?

Origfilename? As? String?

FILECOUNT? AS? INTEGER?

FILESTARTNUM? AS? Long?

END? TYPE? 'Define restore information file structure parameters?

PUBLIC? FUNCTION? SPLITFILE (SPLITFILENAME? AS? STRING, BEGINNINGNUMBER? AS? Long, returnerrordes? As? String, split? String?, Oname? As? String)? Bolean?

'Define the public process, used to divide the file, the function returns a logical value. True represents success, False represents failed, four parameters are in turn, are split file name, start number, division file size, divided file name?

DIM? SAVENAME? AS? STRING? 'Division after division?

DIM? FNUM? AS? INTEGER, FNUM1? AS? INTEGER? 'File channel variable?

SplitFile = True? 'If there is no error below, return true value?

ON? ERROR? GOTO? CLEANUP?

Dim? Currentfile? As? Sectionedfile, m_lngnumfil? As? Long, m_lngloop? As? Long, fileslen? As? Long?

Fileslen = filelen (splitFilename)? Get the size of the filed file?

IF? Fileslen? <=? Split? ? 1? THEN?

Splitfile = false?

ReturnerRordes = "Split file size is less than divided, please reset!"?

EXIT? FUNCTION?

END? If? 'If the divided file is less than the division file, exit the process?

Fnum = freefile? 'Returns the first idle channel to read?

Open? Splitfilename? For? Binary? As? FNUM? 'Use binary to open the split file?

IF? Cint (fileslen / split)> = fileslen / split? Or? Cint (fileslen / split) = fileslen / split? Then?

M_Lngnumfil = CINT (Fileslen / Split)?

Else?

M_Lngnumfil = CINT (fileslen / split) 1?

End? If? 'Precise calculated files will be splitted?

Redim? Currentfile.files (1)? 'Assign a memory-assist array?

For? M_lngloop = 1? To? M_Lngnumfil?

? 'This loop is used to split the file and generate a split file?

IF? M_LNGLOOP?

Redim? Currentfile.files (1) .BYTES (1? To? Split)?

'? Redistribute size equal to the memory array of divided sizes?

GET? # Fnum, currentfile.files (1) .betes?

'Read equal to the binary data to the divided size to the memory array?

Else? 'If it is the last cycle?

Redim? Currentfile.files (1) .BYTES? TO? FILESLEN - (M_LNGNUMFIL-1) * split)? 'Redistributed size equal to the remaining memory array?

GET? # Fnum, currentfile.files (1) .betes?

Close? #Fnum? 'Close the read file channel?

END? IF?

Savename = oname? &? "." &? Format (BeginningNumper-1 M_LNGLOOP, "00 #")? 'Calculates the divided file name, extension is 00? ?

Fnum1 = freefile? 'Get the second idle channel to write?

Open? Savename? For? Binary? As? Fnum1?

PUT? # Fnum1, 1, currentfile.files (1)?

DoEvents?

Close? #Fnum1? 'Written in divided files with binary?

Form1.command6.caption = formatpercent (m_lngloop? /? M_Lngnumfil)? Show simple progress instructions?

Form1.command5.caption = savename? 'Displays the file name being operating?

NEXT?

Dim? Fileinfofile? As? Fileinfo? Defines the content of restore information file?

FileInfofile.FileCount = m_Lngnumfil? 'Divided files after division?

FileInfofile.origfilename = oname? 'Output file name?

Fileinfofile.origprojsize = filelen (splitfilename)? Split file size?

FILEINFOFILE.FILESTARTNUM = BeginningNumber? 'Document start number after division?

Savename = oname? &? ". Hj"? 'Restore information file name?

Fnum = freefile?

Open? Savename? For? Binary? As? #Fnum?

PUT? # Fnum,, fileinfofile?

Close? #Fnum? 'Write to restore information file?

EXIT? FUNCTION?

Cleanup :? 'If there is an error?

ReturnerRORDES = Err.Description?

SplitFile = false? 'Return value is false?

End? Function?

Publicfunctionreassemblefile (Template? Filename? As? String, optional? Useoldfilenameasboolean = true, outputname? As? String) as? Boolean? 'Defines the file restore operation utility?

ON? ERROR? GOTO? E?

FORM1.MOUSEPOINTER = 11?

DIM? Fileinfo? As? Fileinfo, outname? As? String, file? As? Sectionedfile, m_lngloop? As? Long, openname?

DIM? FNUM? AS? INTEGER, FNUM1? AS? INTEGER? INTEGER?

ReassembleFile = TRUE?

Fnum = freefile?

Open? TemplateFileName? For? Binary? As? #Fnum?

GET? # Fnum,, fileinfo?

CLOSE? #Fnum? 'Read the contents of the restore information file?

IF? Useoldfilename? THEN?

Outname = fileinfo.origfilename? Else?

Outname = OUTPUTNAME?

End? If? 'Do you specify your reputation file name?

REDIM? FILE.FILES (1)?

Fnum1 = freefile?

Open? OUTNAME? FOR? Binary? As? #Fnum1?

For? M_lngloop = 1? To? Fileinfo.filecount?

'Determine the number of readings by the number of files recorded in the restore information?

OpenName = fileinfo.origfilename? &? "."? &? Format ((fileInfo.filestartnum-1 m_lngloop), "00 #")?

'Get the file name, path?

Fnum = freefile?

Open? Openname? For? Binary? As? #Fnum?

GET? # Fnum, 1, file.files (1)?

CLOSE? #Fnum? 'Read?

PUT? # Fnum1, file.files (1) .BYTES? 'Write?

Form1.comMand6.caption = formatpercent (m_lngloop / fileinfo.filecount)?

Form1.command5.caption = OpenName?

NEXT?

Close? #Fnum1?

Form1.MousePointer = 0?

EXIT? FUNCTION?

E:??

MSGBOX? "File operation generated error:"? &? Err.Description, vbexclamation?

Reassemblefile = false?

Form1.MousePointer = 0?

End? Function?

(3) Run debugging:?

Firstly, by browsing the specified file name, divided file size, division file name or restore information file name, restore file name, then click Start Split or Start Restore, the program algorithm is rigorous, but in order to keep certain Extend capabilities and compatibility with previous source code also have some redundant code, readers can improve according to their own preferences. In addition, friends who use VB5 pay attention, this function Formatpercent function can only be used in VB6, its role is to convert an expression into a percentage, you must adopt other workarounds, all other code is all universal. ?

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

New Post(0)