| 1 |
(* ***** BEGIN LICENSE BLOCK ***** |
| 2 |
* Compress item to .zipx archive |
| 3 |
* |
| 4 |
* Copyright (C) 2015-2023 Alexander Koblov ([email protected]) |
| 5 |
* |
| 6 |
* Permission is hereby granted, free of charge, to any person obtaining |
| 7 |
* a copy of this software and associated documentation files (the |
| 8 |
* "Software"), to deal in the Software without restriction, including |
| 9 |
* without limitation the rights to use, copy, modify, merge, publish, |
| 10 |
* distribute, sublicense, and/or sell copies of the Software, and to |
| 11 |
* permit persons to whom the Software is furnished to do so, subject to |
| 12 |
* the following conditions: |
| 13 |
|
| 14 |
* The above copyright notice and this permission notice shall be included |
| 15 |
* in all copies or substantial portions of the Software. |
| 16 |
|
| 17 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 20 |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 21 |
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 22 |
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 23 |
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 |
* |
| 25 |
* ***** END LICENSE BLOCK ***** *) |
| 26 |
|
| 27 |
{**********************************************************} |
| 28 |
{* ABBREVIA: AbZipxPrc.pas *} |
| 29 |
{**********************************************************} |
| 30 |
{* ABBREVIA: TZipHashStream class *} |
| 31 |
{**********************************************************} |
| 32 |
|
| 33 |
unit AbZipxPrc; |
| 34 |
|
| 35 |
{$mode delphi} |
| 36 |
|
| 37 |
interface |
| 38 |
|
| 39 |
uses |
| 40 |
Classes, SysUtils, BufStream, AbZipTyp, AbArcTyp; |
| 41 |
|
| 42 |
type |
| 43 |
|
| 44 |
{ TZipHashStream } |
| 45 |
|
| 46 |
TZipHashStream = class(TReadBufStream) |
| 47 |
private |
| 48 |
FSize: Int64; |
| 49 |
FHash: UInt32; |
| 50 |
FOnProgress: TAbProgressEvent; |
| 51 |
public |
| 52 |
constructor Create(ASource : TStream); reintroduce; |
| 53 |
function Read(var ABuffer; ACount : LongInt) : Integer; override; |
| 54 |
property OnProgress : TAbProgressEvent read FOnProgress write FOnProgress; |
| 55 |
property Hash: UInt32 read FHash; |
| 56 |
end; |
| 57 |
|
| 58 |
procedure DoCompressXz(Archive : TAbZipArchive; Item : TAbZipItem; OutStream, InStream : TStream); |
| 59 |
procedure DoCompressZstd(Archive : TAbZipArchive; Item : TAbZipItem; OutStream, InStream : TStream); |
| 60 |
|
| 61 |
implementation |
| 62 |
|
| 63 |
uses |
| 64 |
AbXz, AbZstd, AbExcept, DCcrc32; |
| 65 |
|
| 66 |
procedure DoCompressXz(Archive : TAbZipArchive; Item : TAbZipItem; OutStream, InStream : TStream); |
| 67 |
var |
| 68 |
ASource: TZipHashStream; |
| 69 |
CompStream: TXzCompressionStream; |
| 70 |
begin |
| 71 |
Item.CompressionMethod := cmXz; |
| 72 |
ASource := TZipHashStream.Create(InStream); |
| 73 |
try |
| 74 |
ASource.OnProgress := Archive.OnProgress; |
| 75 |
CompStream := TXzCompressionStream.Create(OutStream, Archive.CompressionLevel); |
| 76 |
try |
| 77 |
CompStream.CopyFrom(ASource, Item.UncompressedSize); |
| 78 |
finally |
| 79 |
CompStream.Free; |
| 80 |
end; |
| 81 |
Item.CRC32 := LongInt(ASource.Hash); |
| 82 |
finally |
| 83 |
ASource.Free; |
| 84 |
end; |
| 85 |
end; |
| 86 |
|
| 87 |
procedure DoCompressZstd(Archive: TAbZipArchive; Item: TAbZipItem; OutStream, |
| 88 |
InStream: TStream); |
| 89 |
var |
| 90 |
ASource: TZipHashStream; |
| 91 |
CompStream: TZSTDCompressionStream; |
| 92 |
begin |
| 93 |
Item.CompressionMethod := cmZstd; |
| 94 |
ASource := TZipHashStream.Create(InStream); |
| 95 |
try |
| 96 |
ASource.OnProgress := Archive.OnProgress; |
| 97 |
CompStream := TZSTDCompressionStream.Create(OutStream, Archive.CompressionLevel, Item.UncompressedSize); |
| 98 |
try |
| 99 |
CompStream.CopyFrom(ASource, Item.UncompressedSize); |
| 100 |
finally |
| 101 |
CompStream.Free; |
| 102 |
end; |
| 103 |
Item.CRC32 := LongInt(ASource.Hash); |
| 104 |
finally |
| 105 |
ASource.Free; |
| 106 |
end; |
| 107 |
end; |
| 108 |
|
| 109 |
{ TZipHashStream } |
| 110 |
|
| 111 |
constructor TZipHashStream.Create(ASource: TStream); |
| 112 |
begin |
| 113 |
FSize := ASource.Size; |
| 114 |
inherited Create(ASource); |
| 115 |
end; |
| 116 |
|
| 117 |
function TZipHashStream.Read(var ABuffer; ACount: LongInt): Integer; |
| 118 |
var |
| 119 |
Abort: Boolean = False; |
| 120 |
begin |
| 121 |
Result := inherited Read(ABuffer, ACount); |
| 122 |
FHash := crc32_16bytes(@ABuffer, Result, FHash); |
| 123 |
if Assigned(FOnProgress) then |
| 124 |
begin |
| 125 |
FOnProgress(GetPosition * 100 div FSize, Abort); |
| 126 |
if Abort then raise EAbUserAbort.Create; |
| 127 |
end; |
| 128 |
end; |
| 129 |
|
| 130 |
end. |
| 131 |
|