今天寫了四個小函數
Dir
Doc
ZipDir
UnZipDoc
函數及測試代碼(分別在 Delphi
unit Unit
interface
uses
Windows
Dialogs
type
TForm
Button
Button
Button
Button
procedure Button
procedure Button
procedure Button
procedure Button
end;
var
Form
implementation
{$R *
uses ActiveX
{把指定文件夾下的文件保存到一個復合文件}
function Dir
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))
if FindFirst(SourcePath +
begin
repeat
if sr
if (sr
begin
Stg
ms := TMemoryStream
ms
ms
Stm
ms
end;
until (FindNext(sr) <>
end;
Result := True;
end;
{上一個 Dir
function Doc
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))
Stg
while True do
begin
EnumStatStg
if (i =
Stg
ms := TMemoryStream
ms
Stm
ms
ms
end;
Result := True;
end;
{把指定文件夾下的文件壓縮到一個復合文件}
function ZipDir
const
Mode = STGM_CREATE or STGM_WRITE or STGM_SHARE_EXCLUSIVE;
var
sr: TSearchRec;
Stg: IStorage;
Stm: IStream;
ms
zip: TCompressionStream;
num: Int
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))
if FindFirst(SourcePath +
begin
repeat
if sr
if (sr
begin
Stg
ms
ms
ms
num := ms
ms
zip := TCompressionStream
ms
zip
ms
Stm
ms
ms
end;
until (FindNext(sr) <>
end;
Result := True;
end;
{上一個 ZipDir
function UnZipDoc
const
Mode = STGM_READ or STGM_SHARE_EXCLUSIVE;
var
Stg: IStorage;
Stm: IStream;
StatStg: TStatStg;
EnumStatStg: IEnumStatStg;
ms
i: Integer;
num: Int
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))
Stg
while True do
begin
EnumStatStg
if (i =
Stg
ms
ms
Stm
ms
ms
ms
ms
UnZip := TDecompressionStream
ms
UnZip
UnZip
ms
ms
ms
end;
Result := True;
end;
{測試 Dir
procedure TForm
const
TestPath =
TestFile =
begin
if Dir
ShowMessage(
end;
{測試 Doc
procedure TForm
const
TestPath =
TestFile =
begin
if Doc
ShowMessage(
end;
{測試 ZipDir
procedure TForm
const
TestPath =
TestFile =
begin
if ZipDir
ShowMessage(
end;
{測試 UnZipDoc
procedure TForm
const
TestPath =
TestFile =
begin
if UnZipDoc
ShowMessage(
end;
From:http://tw.wingwit.com/Article/program/Delphi/201311/8403.html