二级DELPHI控件:文件_流的加密解密方法计算机二级考试

文章作者 100test 发表时间 2009:04:30 03:54:25
来源 100Test.Com百考试题网


  2009年下半年全国计算机等级考试你准备好了没?考计算机等级考试的朋友,2009年下半年全国计算机等级考试时间是2009年9月19日至23日。更多优质资料尽在百考试题论坛 百考试题在线题库
  unit uCompress.
  interface
  uses
  Windows, Messages, SysUtils, Variants, Classes, ComCtrls, Dialogs.
  // 压缩/解压文件
  procedure CompressFile(Source, Target: String). stdcall.
  procedure DecompressFile(Source, Target: String). stdcall.
  // 压缩/解压文件到流
  procedure CompressToStream(FileName: String. Stream: TStream). stdcall.
  procedure DecompressToStream(FileName: String. Stream: TStream). stdcall.
  // 压缩/解压流
  procedure CompressStream(InStream, OutStream: TStream). stdcall.
  procedure DecompressStream(InStream, OutStream: TStream). stdcall.
  implementation
  uses
  ZLib.
  const
  COMPRESS_ERROR = ’压缩文件时出现内部错误:’.
  DECOMPRESS_ERROR = ’解压文件时出现内部错误:’.
  COMPRESS_STRM_ERROR = ’压缩流时出现内部错误:’.
  DECOMPRESS_STRM_ERROR = ’解压流时出现内部错误:’.
  BufSize = $4096.  
  // 压缩文件
  procedure CompressFile(Source, Target: String).
  var
  i: Integer.
  Buf: array[0..BufSize] of byte.
  ComStream: TCompressionStream.
  InStream, OutStream: TFileStream.
  begin
  if not FileExists(Source) then
  Exit.
  InStream := Nil.
  OutStream := nil.
  ComStream := nil.
  try
  // 生成流
  InStream := TFileStream.Create(Source, fmOpenRead OR fmShareDenyNone).
  OutStream := TFileStream.Create(Target, fmCreate OR fmShareDenyWrite).
  ComStream := TCompressionStream.Create(clMax, OutStream).
  // 压缩流
  for i := 1 to (InStream.Size div BufSize) do begin
  InStream.ReadBuffer(Buf, BufSize).
  ComStream.Write(Buf, BufSize).
  end.
  i := InStream.Size mod BufSize.
  if (i >. 0) then begin
  InStream.ReadBuffer(Buf, i).
  ComStream.Write(Buf, i).
  End.
  InStream.Free.
  InStream := nil.
  // 注先后
  ComStream.Free.
  ComStream := nil.
  // 在此写加密流代码(要先释放 ComStream)
  // EncryptStream(OutStream).
  OutStream.Free.
  OutStream := nil.
  except
  on E: Exception do begin
  if (InStream <.>. nil) then
  InStream.Free.
  if (OutStream <.>. nil) then
  OutStream.Free.
  if (ComStream <.>. nil) then
  ComStream.Free.
  MessageDlg(COMPRESS_ERROR #10 E.Message, mtError, [mbOk], 0).
  end.
  end.
  end.

相关文章


DELPHI控件:FindWindowEX的一个应用实例计算机二级考试
计算机二级DELPHI控件:标题窗体拖动计算机二级考试
DELPHI控件:delphiRichEdit中实现url计算机二级考试
二级DELPHI控件:文件_流的加密解密方法计算机二级考试
C语言辅导:DELphiProxies单元解决方法计算机二级考试
DELPHI控件:DELPHI文字滚屏控件(SliderMemo)计算机二级考试
澳大利亚华人论坛
考好网
日本华人论坛
华人移民留学论坛
英国华人论坛