복잡한 구조화 접근 (3): 접근 함수

7276 단어 함수.
오늘은 네 개의 작은 함수를 써서 여러분과 공유합니다.
Dir2Doc: 폴더 아래의 모든 파일(하위 폴더 포함)을 하나의 복합 파일로 저장합니다.
Doc2Dir: Dir2Doc의 반동작,
ZipDir2Doc: Dir2Doc와 같이 압축만 수행합니다.
UnZipDoc2Dir: ZipDir2Doc의 반동작.
함수 및 테스트 코드(Delphi 2007 및 Delphi 2009에서 각각 테스트 통과):


unit Unit1;



interface



uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls;



type

  TForm1 = class(TForm)

    Button1: TButton;

    Button2: TButton;

    Button3: TButton;

    Button4: TButton;

    procedure Button1Click(Sender: TObject);

    procedure Button2Click(Sender: TObject);

    procedure Button3Click(Sender: TObject);

    procedure Button4Click(Sender: TObject);

  end;



var

  Form1: TForm1;



implementation



{$R *.dfm}



uses ActiveX, Zlib; {       }



{                   }

function Dir2Doc(SourcePath, DestFile: string): Boolean;

const

  Mode = STGM_CREATE or STGM_WRITE or STGM_SHARE_EXCLUSIVE;

var

  sr: TSearchRec;

  Stg: IStorage;

  Stm: IStream;

  ms: TMemoryStream;

begin

  Result := False;

  SourcePath := ExcludeTrailingPathDelimiter(SourcePath);        {       '\'}

  if not DirectoryExists(SourcePath) then Exit;                  {           }



  if not DirectoryExists(ExtractFileDir(DestFile)) then          {         }

    if not ForceDirectories(ExtractFileDir(DestFile)) then Exit; {   ,        .}



  {            }



  StgCreateDocfile(PWideChar(WideString(DestFile)), Mode, 0, Stg); {         }



  if FindFirst(SourcePath + '\*.*', faAnyFile, sr) = 0 then

  begin

    repeat

      if sr.Name[1] = '.' then Continue; {   '.'   '..' (         )   }

      if (sr.Attr and faDirectory) <> faDirectory then

      begin

        Stg.CreateStream(PWideChar(WideString(sr.Name)), Mode, 0, 0, Stm);

        ms := TMemoryStream.Create;

        ms.LoadFromFile(SourcePath + '\' + sr.Name);

        ms.Position := 0;

        Stm.Write(ms.Memory, ms.Size, nil);

        ms.Free;

      end;

    until (FindNext(sr) <> 0);

  end;

  Result := True;

end;



{    Dir2Doc       }

function Doc2Dir(SourceFile, DestPath: string): Boolean;

const

  Mode = STGM_READ or STGM_SHARE_EXCLUSIVE;

var

  Stg: IStorage;

  Stm: IStream;

  StatStg: TStatStg;

  EnumStatStg: IEnumStatStg;

  ms: TMemoryStream;

  i: Integer;

begin

  Result := False;

  if not FileExists(SourceFile) then Exit;       {         }

  if not DirectoryExists(DestPath) then          {         }

    if not ForceDirectories(DestPath) then Exit; {   ,        }



  DestPath := ExcludeTrailingPathDelimiter(DestPath); {       '\'}



  StgOpenStorage(PWideChar(WideString(SourceFile)), nil, Mode, nil, 0, Stg);

  Stg.EnumElements(0, nil, 0, EnumStatStg);



  while True do

  begin

    EnumStatStg.Next(1, StatStg, @i);

    if (i = 0) or (StatStg.dwType = 1) then Break; {dwType = 1      }

    Stg.OpenStream(StatStg.pwcsName, nil, Mode, 0, Stm);

    ms := TMemoryStream.Create;

    ms.SetSize(StatStg.cbSize);

    Stm.Read(ms.Memory, ms.Size, nil);

    ms.SaveToFile(DestPath + '\' + StatStg.pwcsName);

    ms.Free;

  end;

  Result := True;

end;



{                   }

function ZipDir2Doc(SourcePath, DestFile: string): Boolean;

const

  Mode = STGM_CREATE or STGM_WRITE or STGM_SHARE_EXCLUSIVE;

var

  sr: TSearchRec;

  Stg: IStorage;

  Stm: IStream;

  ms1,ms2: TMemoryStream;

  zip: TCompressionStream;

  num: Int64;

begin

  Result := False;

  SourcePath := ExcludeTrailingPathDelimiter(SourcePath);        {       '\'}

  if not DirectoryExists(SourcePath) then Exit;                  {           }

  if not DirectoryExists(ExtractFileDir(DestFile)) then          {         }

    if not ForceDirectories(ExtractFileDir(DestFile)) then Exit; {   ,        .}



  StgCreateDocfile(PWideChar(WideString(DestFile)), Mode, 0, Stg); {         }



  if FindFirst(SourcePath + '\*.*', faAnyFile, sr) = 0 then

  begin

    repeat

      if sr.Name[1] = '.' then Continue; {   '.'   '..' (         )   }

      if (sr.Attr and faDirectory) <> faDirectory then

      begin

        Stg.CreateStream(PWideChar(WideString(sr.Name)), Mode, 0, 0, Stm);

        ms1 := TMemoryStream.Create;

        ms2 := TMemoryStream.Create;

        ms1.LoadFromFile(SourcePath + '\' + sr.Name);



        num := ms1.Size;

        ms2.Write(num, SizeOf(num));

        zip := TCompressionStream.Create(clMax, ms2);

        ms1.SaveToStream(zip);

        zip.Free;



        ms2.Position := 0;

        Stm.Write(ms2.Memory, ms2.Size, nil);



        ms1.Free;

        ms2.Free;

      end;

    until (FindNext(sr) <> 0);

  end;

  Result := True;

end;



{    ZipDir2Doc       }

function UnZipDoc2Dir(SourceFile, DestPath: string): Boolean;

const

  Mode = STGM_READ or STGM_SHARE_EXCLUSIVE;

var

  Stg: IStorage;

  Stm: IStream;

  StatStg: TStatStg;

  EnumStatStg: IEnumStatStg;

  ms1,ms2: TMemoryStream;

  i: Integer;

  num: Int64;

  UnZip: TDecompressionStream;

begin

  Result := False;

  if not FileExists(SourceFile) then Exit;    {         }

  if not DirectoryExists(DestPath) then          {         }

    if not ForceDirectories(DestPath) then Exit; {   ,        }



  DestPath := ExcludeTrailingPathDelimiter(DestPath); {       '\'}



  StgOpenStorage(PWideChar(WideString(SourceFile)), nil, Mode, nil, 0, Stg);

  Stg.EnumElements(0, nil, 0, EnumStatStg);



  while True do

  begin

    EnumStatStg.Next(1, StatStg, @i);

    if (i = 0) or (StatStg.dwType = 1) then Break; {dwType = 1      }

    Stg.OpenStream(StatStg.pwcsName, nil, Mode, 0, Stm);

    ms1 := TMemoryStream.Create;

    ms1.SetSize(StatStg.cbSize);

    Stm.Read(ms1.Memory, ms1.Size, nil);

    ms1.Position := 0;

    ms1.ReadBuffer(num, SizeOf(num));

    ms2 := TMemoryStream.Create;

    ms2.SetSize(num);



    UnZip := TDecompressionStream.Create(ms1);

    ms2.Position := 0;

    UnZip.Read(ms2.Memory^, num);

    UnZip.Free;



    ms2.SaveToFile(DestPath + '\' + StatStg.pwcsName);

    ms1.Free;

    ms2.Free;

  end;

  Result := True;

end;



{   Dir2Doc}

procedure TForm1.Button1Click(Sender: TObject);

const

  TestPath = 'C:\Documents and Settings\All Users\Documents\My Pictures\    ';

  TestFile = 'C:\Temp\pic1.dat';

begin

  if Dir2Doc(TestPath, TestFile) then

    ShowMessage('ok');

end;



{   Doc2Dir}

procedure TForm1.Button2Click(Sender: TObject);

const

  TestPath = 'C:\Temp\pic1';

  TestFile = 'C:\Temp\pic1.dat';

begin

  if Doc2Dir(TestFile, TestPath) then

    ShowMessage('ok');

end;



{   ZipDir2Doc}

procedure TForm1.Button3Click(Sender: TObject);

const

  TestPath = 'C:\Documents and Settings\All Users\Documents\My Pictures\    ';

  TestFile = 'C:\Temp\pic2.dat';

begin

  if ZipDir2Doc(TestPath, TestFile) then

    ShowMessage('ok');

end;



{   UnZipDoc2Dir}

procedure TForm1.Button4Click(Sender: TObject);

const

  TestPath = 'C:\Temp\pic2';

  TestFile = 'C:\Temp\pic2.dat';

begin

  if UnZipDoc2Dir(TestFile, TestPath) then

    ShowMessage('ok');

end;



end.


 
   

좋은 웹페이지 즐겨찾기