📄 jvqzlibmultiple.pas
字号:
{******************************************************************************}
{* WARNING: JEDI VCL To CLX Converter generated unit. *}
{* Manual modifications will be lost on next release. *}
{******************************************************************************}
{-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is: JvZlibMultiple.PAS, released on 2001-02-28.
The Initial Developer of the Original Code is S閎astien Buysse [sbuysse att buypin dott com]
Portions created by S閎astien Buysse are Copyright (C) 2001 S閎astien Buysse.
All Rights Reserved.
Contributor(s): Michael Beck [mbeck att bigfoot dott com].
You may retrieve the latest version of this file at the Project JEDI's JVCL home page,
located at http://jvcl.sourceforge.net
Known Issues:
2004-07-27 - Read the 'ALL USERS READ THIS' section below.
-----------------------------------------------------------------------------}
// $Id: JvQZlibMultiple.pas,v 1.17 2004/12/21 09:45:20 asnepvangers Exp $
{$I jvcl.inc}
unit JvQZlibMultiple;
interface
uses
SysUtils, Classes, QGraphics, QControls, QDialogs,
JclCompression,
JvQComponent;
// ----------------------------------------------------------------------------}
// 2004-07-27 *** ALL USERS READ THIS: (wpostma) ***
//
// I have added support for selective extraction and listing archive contents without
// writing any files to disk. To do this we had to add some parameters to the component events.
//
// This will break existing applications that use these events, until they update their
// event declarations, to add the new parameters to your events.
//
// This is something the Delphi IDE should do automatically, but does not do. <grin>
//
// The old events for OnDecompressingFile, and OnDecompressedFile
// look like this:
// procedure <<TMyForm.MyEventHandlerName>>(Sender: TObject; const FileName: string)
//
// The new events have an additional parameter each:
//
// OnDecompressingFile -> (Sender: TObject; const FileName: string;
// {NEW!} var WriteFile: Boolean )
// OnDecompressedFile -> (Sender: TObject; const FileName: string;
// {NEW!} const FileSize: Longword )
//
// -----------------------------------------------------------------------------}
type
{NEW:}
TFileBeforeWriteEvent = procedure(Sender: TObject; const FileName: string; var WriteFile: Boolean) of object;
TFileAfterWriteEvent = procedure(Sender: TObject; const FileName: string; const FileSize: Longword) of object;
TFileEvent = procedure(Sender: TObject; const FileName: string) of object;
TProgressEvent = procedure(Sender: TObject; Position, Total: Integer) of object;
TJvZlibMultiple = class(TJvComponent)
private
FStorePaths: Boolean;
FOnProgress: TProgressEvent;
FOnCompressingFile: TFileEvent;
FOnCompressedFile: TFileEvent;
FOnCompletedAction: TNotifyEvent;
// July 26, 2004: New improved event types for decompression: Allow user to
// skip writing of files they want skipped on extraction, and if they
// extract nothing, they can use this "nil extraction" to scan the contents
// of the file, returning the file names and sizes inside.
FOnDecompressingFile: TFileBeforeWriteEvent;
FOnDecompressedFile: TFileAfterWriteEvent;
protected
procedure AddFile(FileName, Directory, FilePath: string; DestStream: TStream);
procedure DoProgress(Position, Total: Integer); virtual;
public
constructor Create(AOwner: TComponent); override;
// compresses a list of files (can contain wildcards)
// NOTE: caller must free returned stream!
function CompressFiles(Files: TStrings): TStream; overload;
// compresses a list of files (can contain wildcards)
// and saves the compressed result to FileName
procedure CompressFiles(Files: TStrings; FileName: string); overload;
// compresses a Directory (recursing if Recursive is true)
// NOTE: caller must free returned stream!
function CompressDirectory(Directory: string; Recursive: Boolean): TStream; overload;
// compresses a Directory (recursing if Recursive is true)
// and saves the compressed result to FileName
procedure CompressDirectory(Directory: string; Recursive: Boolean; FileName: string); overload;
// decompresses FileName into Directory. If Overwrite is true, overwrites any existing files with
// the same name as those in the compressed archive.
// If RelativePaths is true, the paths in the compressed file are stripped from their drive letter
procedure DecompressFile(FileName, Directory: string; Overwrite: Boolean; const RelativePaths: Boolean = True);
// decompresses Stream into Directory optionally overwriting any existing files
// If RelativePaths is true, any paths in the stream are stripped from their drive letter
procedure DecompressStream(Stream: TStream; Directory: string; Overwrite: Boolean; const RelativePaths: Boolean = True);
published
property StorePaths: Boolean read FStorePaths write FStorePaths default True;
// NOTE: Changed decompression event parameters. July 26 2004. -WPostma.
property OnDecompressingFile: TFileBeforeWriteEvent read FOnDecompressingFile write FOnDecompressingFile;
property OnDecompressedFile: TFileAfterWriteEvent read FOnDecompressedFile write FOnDecompressedFile;
property OnProgress: TProgressEvent read FOnProgress write FOnProgress;
property OnCompressingFile: TFileEvent read FOnCompressingFile write FOnCompressingFile;
property OnCompressedFile: TFileEvent read FOnCompressedFile write FOnCompressedFile;
property OnCompletedAction: TNotifyEvent read FOnCompletedAction write FOnCompletedAction;
end;
implementation
uses
{$IFDEF UNITVERSIONING}
JclUnitVersioning,
{$ENDIF UNITVERSIONING}
JvQJCLUtils;
{*******************************************************}
{ Format of the File: }
{ File Header }
{ 1 Byte Size of the directory variable }
{ x bytes Directory of the file }
{ 1 Byte Size of the filename }
{ x bytes Filename }
{ 4 bytes Size of the file (uncompressed) }
{ 4 bytes Size of the file (compressed) }
{ Data chunk }
{ x bytes the compressed chunk }
{*******************************************************}
constructor TJvZlibMultiple.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FStorePaths := True;
end;
function TJvZlibMultiple.CompressDirectory(Directory: string; Recursive: Boolean): TStream;
procedure SearchDirectory(SDirectory: string);
var
SearchRec: TSearchRec;
Res: Integer;
begin
// (rom) this may not work for network drives and compressed files
// (rom) because of faAnyFile
Res := FindFirst(Directory + SDirectory + AllFilesMask, faAnyFile, SearchRec);
try
while Res = 0 do
begin
if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
begin
if (SearchRec.Attr and faDirectory) = 0 then
AddFile(SearchRec.Name, SDirectory, Directory + SDirectory + SearchRec.Name, Result)
else
if Recursive then
SearchDirectory(SDirectory + SearchRec.Name + PathDelim);
end;
Res := FindNext(SearchRec);
end;
finally
FindClose(SearchRec);
end;
end;
begin
{ (RB) Letting this function create a stream is not a good idea;
see other CompressDirectory function that causes a memory leak }
Result := TMemoryStream.Create;
Directory := IncludeTrailingPathDelimiter(Directory);
SearchDirectory('');
Result.Position := 0;
end;
procedure TJvZlibMultiple.AddFile(FileName, Directory, FilePath: string;
DestStream: TStream);
var
Stream: TStream;
FileStream: TFileStream;
ZStream: TJclZLibCompressStream;
Buffer: array [0..1023] of Byte;
Count: Integer;
procedure WriteFileRecord(Directory, FileName: string; FileSize: Integer;
CompressedSize: Integer);
var
B: Byte;
Tab: array [1..256] of Char;
begin
{ (RB) Can be improved }
for B := 1 to Length(Directory) do
Tab[B] := Directory[B];
B := Length(Directory);
DestStream.Write(B, SizeOf(B));
DestStream.Write(Tab, B);
{ (RB) Can be improved }
for B := 1 to Length(FileName) do
Tab[B] := FileName[B];
B := Length(FileName);
DestStream.Write(B, SizeOf(B));
DestStream.Write(Tab, B);
DestStream.Write(FileSize, SizeOf(FileSize));
DestStream.Write(CompressedSize, SizeOf(CompressedSize));
end;
begin
Stream := TMemoryStream.Create;
FileStream := TFileStream.Create(FilePath, fmOpenRead or fmShareDenyWrite);
try
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -