Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 55 additions & 31 deletions KaZip.pas
Original file line number Diff line number Diff line change
Expand Up @@ -760,26 +760,41 @@ procedure Register;
RegisterComponents('KA', [TKAZip]);
end;

function zs2s(value: TZipString): string;
begin
Result := value;
end;

function as2s(value: RawByteString): string;
begin
Result := value;
end;

function s2zs(value: string): TZipString;
begin
Result := value;
end;

function ToZipName(FileName: UnicodeString): TZipString;
var
P: Integer;
begin
Result := FileName;
Result := StringReplace(Result, '\', '/', [rfReplaceAll]);
P := Pos(':/', Result);
Result := s2zs(FileName);
Result := s2zs(StringReplace(zs2s(Result), '\', '/', [rfReplaceAll]));
P := Pos(':/', zs2s(Result));
if P > 0 then
begin
System.Delete(Result, 1, P + 1);
end;
P := Pos('//', Result);
P := Pos('//', zs2s(Result));
if P > 0 then
begin
System.Delete(Result, 1, P + 1);
P := Pos('/', Result);
P := Pos('/', zs2s(Result));
if P > 0 then
begin
System.Delete(Result, 1, P);
P := Pos('/', Result);
P := Pos('/', zs2s(Result));
if P > 0 then
System.Delete(Result, 1, P);
end;
Expand Down Expand Up @@ -1015,7 +1030,7 @@ function TKAZipEntriesEntry.Test: Boolean;

procedure TKAZipEntriesEntry.SetComment(const Value: UnicodeString);
begin
FCentralDirectoryFile.FileComment := Value;
FCentralDirectoryFile.FileComment := s2zs(Value);
FCentralDirectoryFile.FileCommentLength := Length(FCentralDirectoryFile.FileComment);
FParent.Rebuild;
if not FParent.FParent.FBatchMode then
Expand All @@ -1028,11 +1043,11 @@ procedure TKAZipEntriesEntry.SetFileName(const Value: UnicodeString);
var
fn: string;
begin
fn := ToZipName(Value);
fn := zs2s(ToZipName(Value));
if FParent.IndexOf(FN) > -1 then
raise EKaZipException.Create('File with same name already exists in Archive');

FCentralDirectoryFile.FileName := fn;
FCentralDirectoryFile.FileName := s2zs(fn);
FCentralDirectoryFile.FilenameLength := Length(fn);
if not FParent.FParent.FBatchMode then
begin
Expand Down Expand Up @@ -1477,7 +1492,9 @@ function TKAZipEntries.ParseLocalHeaders(MS: TStream): Boolean;
CDFile.FileCommentLength := 0;
CDFile.DiskNumberStart := 0;
CDFile.InternalFileAttributes := LocalFile.VersionNeededToExtract;
{$WARN SYMBOL_PLATFORM OFF}
CDFile.ExternalFileAttributes := faArchive;
{$WARN SYMBOL_PLATFORM ON}
CDFile.RelativeOffsetOfLocalHeader := Poz;
CDFile.FileName := LocalFile.FileName;
L := Length(CDFile.FileName);
Expand Down Expand Up @@ -1754,10 +1771,11 @@ function TKAZipEntries.IndexOf(const FileName: string): Integer;
begin
Result := -1;

fn := ToZipName(FileName);
fn := zs2s(ToZipName(FileName));
for X := 0 to Count - 1 do
begin
if AnsiCompareText(fn, ToZipName(Items[X].FCentralDirectoryFile.FileName)) = 0 then
if AnsiCompareText(fn, zs2s(ToZipName(
zs2s(Items[X].FCentralDirectoryFile.FileName)))) = 0 then
begin
Result := X;
Exit;
Expand Down Expand Up @@ -1792,7 +1810,7 @@ function TKAZipEntries.AddStreamFast(ItemName: string; FileAttr: Word; FileDate:
if not FParent.FStoreRelativePath then
ItemName := ExtractFileName(ItemName);

ItemName := ToZipName(ItemName); //standardize ItemName into Zip allowed filenames
ItemName := zs2s(ToZipName(ItemName)); //standardize ItemName into Zip allowed filenames

//If an item with this name already exists then remove it
i := Self.IndexOf(ItemName);
Expand Down Expand Up @@ -1834,7 +1852,7 @@ function TKAZipEntries.AddStreamFast(ItemName: string; FileAttr: Word; FileDate:
Result.FLocalFile.UncompressedSize := uncompressedLength;
Result.FLocalFile.FilenameLength := Length(ItemName);
Result.FLocalFile.ExtraFieldLength := 0;
Result.FLocalFile.FileName := ItemName;
Result.FLocalFile.FileName := s2zs(ItemName);
Result.FLocalFile.ExtraField := '';
Result.FLocalFile.CompressedData := ''; //not used

Expand Down Expand Up @@ -1935,7 +1953,7 @@ function TKAZipEntries.AddStreamFast(ItemName: string; FileAttr: Word; FileDate:
Result.FCentralDirectoryFile.InternalFileAttributes := 0;
Result.FCentralDirectoryFile.ExternalFileAttributes := FileAttr;
Result.FCentralDirectoryFile.RelativeOffsetOfLocalHeader := newLocalEntryPosition;
Result.FCentralDirectoryFile.FileName := ItemName;
Result.FCentralDirectoryFile.FileName := s2zs(ItemName);
Result.FCentralDirectoryFile.ExtraField := '';
Result.FCentralDirectoryFile.FileComment := '';

Expand Down Expand Up @@ -2018,7 +2036,7 @@ function TKAZipEntries.AddStreamRebuild(ItemName: string; FileAttr: Word; FileDa
ZipComment := FParent.Comment.Text;
if not FParent.FStoreRelativePath then
ItemName := ExtractFileName(ItemName);
ItemName := ToZipName(ItemName);
ItemName := zs2s(ToZipName(ItemName));
I := IndexOf(ItemName);
if I > -1 then
begin
Expand Down Expand Up @@ -2086,7 +2104,7 @@ function TKAZipEntries.AddStreamRebuild(ItemName: string; FileAttr: Word; FileDa
UncompressedSize := UL;
FilenameLength := Length(ItemName);
ExtraFieldLength := 0;
FileName := ItemName;
FileName := s2zs(ItemName);
ExtraField := '';
CompressedData := '';
end;
Expand All @@ -2109,7 +2127,7 @@ function TKAZipEntries.AddStreamRebuild(ItemName: string; FileAttr: Word; FileDa
InternalFileAttributes := 0;
ExternalFileAttributes := FileAttr;
RelativeOffsetOfLocalHeader := TempStream.Position;
FileName := ItemName;
FileName := s2zs(ItemName);
ExtraField := '';
FileComment := '';
end;
Expand Down Expand Up @@ -2159,7 +2177,7 @@ function TKAZipEntries.AddStreamRebuild(ItemName: string; FileAttr: Word; FileDa
ZipComment := FParent.Comment.Text;
if not FParent.FStoreRelativePath then
ItemName := ExtractFileName(ItemName);
ItemName := ToZipName(ItemName);
ItemName := zs2s(ToZipName(ItemName));
I := IndexOf(ItemName);
if I > -1 then
begin
Expand Down Expand Up @@ -2227,7 +2245,7 @@ function TKAZipEntries.AddStreamRebuild(ItemName: string; FileAttr: Word; FileDa
UncompressedSize := UL;
FilenameLength := Length(ItemName);
ExtraFieldLength := 0;
FileName := ItemName;
FileName := s2zs(ItemName);
ExtraField := '';
CompressedData := '';
end;
Expand All @@ -2250,7 +2268,7 @@ function TKAZipEntries.AddStreamRebuild(ItemName: string; FileAttr: Word; FileDa
InternalFileAttributes := 0;
ExternalFileAttributes := FileAttr;
RelativeOffsetOfLocalHeader := TempMSStream.Position;
FileName := ItemName;
FileName := s2zs(ItemName);
ExtraField := '';
FileComment := '';
end;
Expand Down Expand Up @@ -2312,7 +2330,7 @@ function TKAZipEntries.AddFolderChain(ItemName: string; FileAttr: Word;
NoMore: Boolean;
begin
// Result := False;
FN := ExtractFilePath(ToDosName(ToZipName(ItemName)));
FN := ExtractFilePath(ToDosName(zs2s(ToZipName(ItemName))));
TN := FN;
INCN := '';
MS := TMemoryStream.Create;
Expand Down Expand Up @@ -2364,7 +2382,9 @@ function TKAZipEntries.AddStream(FileName: string; FileAttr: Word; FileDate: TDa

function TKAZipEntries.AddStream(FileName: string; Stream: TStream): TKAZipEntriesEntry;
begin
{$WARN SYMBOL_PLATFORM OFF}
Result := AddStream(FileName, faArchive, Now, Stream);
{$WARN SYMBOL_PLATFORM ON}
end;

function TKAZipEntries.AddFile(FileName, NewFileName: string): TKAZipEntriesEntry;
Expand Down Expand Up @@ -2627,6 +2647,7 @@ procedure TKAZipEntries.InternalExtractToFile(Item: TKAZipEntriesEntry; FileName
end;
if FParent.FApplyAttributes then
begin
{$WARN SYMBOL_PLATFORM OFF}
Attr := faArchive;
if Item.FCentralDirectoryFile.ExternalFileAttributes and faHidden > 0 then
Attr := Attr or faHidden;
Expand All @@ -2635,6 +2656,7 @@ procedure TKAZipEntries.InternalExtractToFile(Item: TKAZipEntriesEntry; FileName
if Item.FCentralDirectoryFile.ExternalFileAttributes and faReadOnly > 0 then
Attr := Attr or faReadOnly;
FileSetAttr(FileName, Attr);
{$WARN SYMBOL_PLATFORM ON}
end;
end;
end;
Expand Down Expand Up @@ -2836,7 +2858,7 @@ procedure TKAZipEntries.CreateFolder(FolderName: string; FolderDate: TDateTime);
var
FN: string;
begin
FN := IncludeTrailingBackslash(FolderName);
FN := IncludeTrailingPathDelimiter(FolderName);
AddFolderChain(FN, faDirectory, FolderDate);
FParent.FIsDirty := True;
end;
Expand All @@ -2849,8 +2871,8 @@ procedure TKAZipEntries.RenameFolder(FolderName: string; NewFolderName: string);
X: Integer;
L: Integer;
begin
FN := ToZipName(IncludeTrailingBackslash(FolderName));
NFN := ToZipName(IncludeTrailingBackslash(NewFolderName));
FN := zs2s(ToZipName(IncludeTrailingPathDelimiter(FolderName)));
NFN := zs2s(ToZipName(IncludeTrailingPathDelimiter(NewFolderName)));
L := Length(FN);
if IndexOf(NFN) = -1 then
begin
Expand Down Expand Up @@ -2930,7 +2952,7 @@ function TKAZipEntries.AddEntryThroughStream(FileName: string; FileDate: TDateTi
if not FParent.FStoreRelativePath then
itemName := ExtractFileName(itemName);

itemName := ToZipName(itemName);
itemName := zs2s(ToZipName(itemName));

//If an item with this name already exists then remove it
i := Self.IndexOf(itemName);
Expand Down Expand Up @@ -2966,7 +2988,7 @@ function TKAZipEntries.AddEntryThroughStream(FileName: string; FileDate: TDateTi
newEntry.FLocalFile.UncompressedSize := 0; //don't know it yet, will back-fill
newEntry.FLocalFile.FilenameLength := Length(itemName);
newEntry.FLocalFile.ExtraFieldLength := 0;
newEntry.FLocalFile.FileName := ItemName;
newEntry.FLocalFile.FileName := s2zs(ItemName);
newEntry.FLocalFile.ExtraField := '';
newEntry.FLocalFile.CompressedData := ''; //not used here, because we'll be writing directly to a stream later

Expand All @@ -2987,7 +3009,7 @@ function TKAZipEntries.AddEntryThroughStream(FileName: string; FileDate: TDateTi
newEntry.FCentralDirectoryFile.InternalFileAttributes := 0;
newEntry.FCentralDirectoryFile.ExternalFileAttributes := FileAttr;
newEntry.FCentralDirectoryFile.RelativeOffsetOfLocalHeader := newLocalEntryPosition;
newEntry.FCentralDirectoryFile.FileName := itemName;
newEntry.FCentralDirectoryFile.FileName := s2zs(itemName);
newEntry.FCentralDirectoryFile.ExtraField := '';
newEntry.FCentralDirectoryFile.FileComment := '';

Expand Down Expand Up @@ -3740,7 +3762,9 @@ function TKAZip.AddEntryThroughStream(FileName: string): TStream;
stm.Free;
end;
}
{$WARN SYMBOL_PLATFORM OFF}
Result := Entries.AddEntryThroughStream(FileName, Now, faArchive);
{$WARN SYMBOL_PLATFORM ON}
end;

{ TCRC32Stream }
Expand Down Expand Up @@ -3835,7 +3859,7 @@ class procedure TCRC32Stream.SelfTest;
finally
cs.Free;
end;
CheckEqualsString(InputData, ss.DataString);
CheckEqualsString(as2s(InputData), ss.DataString);
finally
ss.Free;
end;
Expand All @@ -3851,13 +3875,13 @@ class procedure TCRC32Stream.SelfTest;
finally
cs.Free;
end;
CheckEqualsString(InputData, ss.DataString);
CheckEqualsString(as2s(InputData), ss.DataString);
finally
ss.Free;
end;

//Check that both chunking methods match
CheckEquals(crc1, crc2, InputData);
CheckEquals(crc1, crc2, as2s(InputData));

//And return one of them to compare to the old way
Result := crc1;
Expand All @@ -3870,7 +3894,7 @@ class procedure TCRC32Stream.SelfTest;
crcOld := Self.CalcCRC32(InputString);
crcNew := CRCNewWay(InputString);

CheckEquals(crcOld, crcNew, InputString);
CheckEquals(crcOld, crcNew, as2s(InputString));
end;

begin
Expand Down