zoukankan      html  css  js  c++  java
  • 文件分割合并

    procedure SplitFile(FileName : TFileName; FilesByteSize : Integer) ;
    // FileName == file to split into several smaller files
    // FilesByteSize == the size of files in bytes
    var
    fs, ss: TFileStream;
    cnt : integer;
    SplitName: String;
    begin
    fs := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite) ;
    try
    for cnt := 1 to Trunc(fs.Size / FilesByteSize) + 1 do
    begin
    SplitName := ChangeFileExt(FileName, Format('%s%d', ['._',cnt])) ;
    ss := TFileStream.Create(SplitName, fmCreate or fmShareExclusive) ;
    try
    if fs.Size - fs.Position < FilesByteSize then
    FilesByteSize := fs.Size - fs.Position;
    ss.CopyFrom(fs, FilesByteSize) ;
    finally
    ss.Free;
    end;
    end;
    finally
    fs.Free;
    end;
    end;

    Note: a 3 KB file 'myfile.ext' will be split into 'myfile._1', 'myfile._2','myfile._3' if FilesByteSize parameter equals 1024 (1 KB).

    procedure MergeFiles(FirstSplitFileName, OutFileName : TFileName) ;
    // FirstSplitFileName == the name of the first piece of the split file
    // OutFileName == the name of the resulting merged file
    var
    fs, ss: TFileStream;
    cnt: integer;
    begin
    cnt := 1;
    fs := TFileStream.Create(OutFileName, fmCreate or fmShareExclusive) ;
    try
    while FileExists(FirstSplitFileName) do
    begin
    ss := TFileStream.Create(FirstSplitFileName, fmOpenRead or fmShareDenyWrite) ;
    try
    fs.CopyFrom(ss, 0) ;
    finally
    ss.Free;
    end;
    Inc(cnt) ;
    FirstSplitFileName := ChangeFileExt(FirstSplitFileName, Format('%s%d', ['._',cnt])) ;
    end;
    finally
    fs.Free;
    end;
    end;

    Usage:
    SplitFile('c:mypicture.bmp', 1024) ; //into 1 KB files 
    ...
    MergeFiles('c:mypicture._1','c:mymergedpicture.bmp') ;

    http://www.cnblogs.com/hnxxcxg/archive/2012/11/04/2753259.html

  • 相关阅读:
    Dubbo简介---搭建一个最简单的Demo框架
    git学习总结
    FastJson对于JSON格式字符串、JSON对象及JavaBean之间的相互转换
    Spring AOP实现Mysql数据库主从切换(一主多从)
    Mybatis中int insertSelective()的相关问题
    主从数据库读写分离知识
    IoC理解
    AOP理解
    MyBatis中mybatis-generator代码生成的一般过程
    fread 快速读入
  • 原文地址:https://www.cnblogs.com/findumars/p/5236961.html
Copyright © 2011-2022 走看看