程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 關於 Delphi 中流的使用(10): 壓縮與解壓縮進度

關於 Delphi 中流的使用(10): 壓縮與解壓縮進度

編輯:Delphi

 本例效果圖:

關於 Delphi 中流的使用(10): 壓縮與解壓縮進度

  代碼文件:

unit Unit1; 
 
interface 
 
uses 
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
 Dialogs, StdCtrls, ComCtrls; 
 
type 
 TForm1 = class(TForm) 
  Button1: TButton; 
  Button2: TButton; 
  ProgressBar1: TProgressBar; 
  procedure Button1Click(Sender: TObject); 
  procedure Button2Click(Sender: TObject); 
  procedure CsProgress(Sender: TObject); {壓縮的 OnProgress 事件} 
  procedure DsProgress(Sender: TObject); {解壓縮的 OnProgress 事件} 
 end; 
 
var 
 Form1: TForm1; 
 
implementation 
 
{$R *.dfm} 
 
uses Zlib; 
 
{壓縮的 OnProgress 事件} 
procedure TForm1.CsProgress(Sender: TObject); 
begin 
 ProgressBar1.Position := Integer(TCompressionStream(Sender).Position div 1024); 
 Application.ProcessMessages; 
end; 
 
{解壓縮的 OnProgress 事件} 
procedure TForm1.DsProgress(Sender: TObject); 
begin 
 ProgressBar1.Position := Integer(TDecompressionStream(Sender).Position div 1024); 
 Application.ProcessMessages; 
end; 
 
{壓縮} 
procedure TForm1.Button1Click(Sender: TObject); 
var 
 cs: TCompressionStream; 
 fs,ms: TMemoryStream; 
 num: Integer; 
begin 
 fs := TMemoryStream.Create; 
 fs.LoadFromFile('c:\temp\test.txt'); {我是用一個 15M 的文本文件測試的} 
 num := fs.Size; 
 
 ms := TMemoryStream.Create; 
 ms.Write(num, SizeOf(num)); 
 
 cs := TCompressionStream.Create(clMax, ms); 
 
 {在原來代碼基礎是添加這兩行} 
 ProgressBar1.Max := Integer(fs.Size div 1024); 
 cs.OnProgress := CsProgress; 
 
 fs.SaveToStream(cs); 
 cs.Free; 
 
 ms.SaveToFile('c:\temp\test.zipx'); 
 
 ms.Free; 
 fs.Free; 
end; 
 
 
{解壓縮} 
procedure TForm1.Button2Click(Sender: TObject); 
var 
 ds: TDecompressionStream; 
 fs,ms: TMemoryStream;   
 num: Integer; 
begin 
 fs := TMemoryStream.Create; 
 fs.LoadFromFile('c:\temp\test.zipx'); 
 fs.Position := 0; 
 fs.ReadBuffer(num,SizeOf(num)); 
 
 ms := TMemoryStream.Create; 
 ms.SetSize(num); 
 
 ds := TDecompressionStream.Create(fs); 
 
 {在原來代碼基礎是添加這兩行} 
 ProgressBar1.Max := Integer(ms.Size div 1024); 
 ds.OnProgress := DsProgress; 
 
 ds.Read(ms.Memory^, num); 
 
 ms.SaveToFile('c:\temp\test2.txt'); 
 
 ds.Free; 
 ms.Free; 
 fs.Free; 
end; 
 
end. 

窗體文件:

object Form1: TForm1 
 Left = 0 
 Top = 0 
 Caption = 'Form1' 
 ClIEntHeight = 136 
 ClIEntWidth = 205 
 Color = clBtnFace 
 Font.Charset = DEFAULT_CHARSET 
 Font.Color = clWindowText 
 Font.Height = -11 
 Font.Name = 'Tahoma' 
 Font.Style = [] 
 OldCreateOrder = False 
 PixelsPerInch = 96 
 TextHeight = 13 
 object Button1: TButton 
  Left = 64 
  Top = 24 
  Width = 75 
  Height = 25 
  Caption = #21387#32553 
  TabOrder = 0 
  OnClick = Button1Click 
 end 
 object Button2: TButton 
  Left = 64 
  Top = 55 
  Width = 75 
  Height = 25 
  Caption = #35299#21387#32553 
  TabOrder = 1 
  OnClick = Button2Click 
 end 
 object ProgressBar1: TProgressBar 
  Left = 24 
  Top = 97 
  Width = 150 
  Height = 17 
  TabOrder = 2 
 end 
end 



  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved