#1 2023-05-05 21:01:02

tbo
Member
Registered: 2015-04-20
Posts: 337

T7zWriter.SaveToFile, Mode error

The following source code for testing. If 7z file exists, open it, otherwise create it:

var zipFileName: TFileName := MakePath([Executable.ProgramFilePath, 'TestDaten.7z']);
if FileExists(zipFileName) then
  libWriter := New7zWriter(zipFileName, fh7z)
else
  libWriter := New7zWriter(fh7z);

For it to work, function SaveToFile must be changed as follows:

begin
  fFileName := DestName;
  // f := TFileStreamEx.Create(DestName, fmCreate);
  if fUpdateReader <> Nil then
    f := TFileStreamEx.Create(DestName, fmOpenWrite or fmShareDenyNone)
  else
    f := TFileStreamEx.Create(DestName, fmCreate);
  try
    SaveToStream(f);

Without this change it makes no sense to open a file for editing because it cannot be saved with SaveToFile.

With best regards
Thomas

Offline

#2 2023-05-06 19:17:01

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,310
Website

Re: T7zWriter.SaveToFile, Mode error

I am not sure it is possible to modify an archive in place, to be honest.

Perhaps the "if fileExists" is the wrong idea.

Offline

#3 2023-05-06 20:57:14

tbo
Member
Registered: 2015-04-20
Posts: 337

Re: T7zWriter.SaveToFile, Mode error

ab wrote:

I am not sure it is possible to modify an archive in place, to be honest.

With my modification for function SaveToFile, it works for this test:

procedure SaveFolderTo7Zip(const pmcFolder, pmc7ZipFileName: TFileName;
  const pmc7ZipInternalDir: TFileName = ''; const pmcFileMask: TFileName = '*';
  pmNew7Zip: Boolean = False; const pmOnProgress: T7zProgressCallback = Nil);
var
  libWriter: I7zWriter;
begin
  if not pmNew7Zip
    and FileExists(pmc7ZipFileName) then
  begin
    libWriter := New7zWriter(pmc7ZipFileName, fh7z)
  end
  else
    libWriter := New7zWriter(fh7z);

  libWriter.SetCompressionLevel(3);
  if Assigned(pmOnProgress) then
    libWriter.SetProgressCallback(pmOnProgress);

  libWriter.AddFiles(pmcFolder, pmc7ZipInternalDir, pmcFileMask, True);
  libWriter.SaveToFile(pmc7ZipFileName);
end;

Call then as follows:

var
  zipFileName: TFileName;
begin
  zipFileName := MakePath([Executable.ProgramFilePath, 'TestMultiData.7z']);
  SaveFolderTo7Zip('c:\mORMot2\src\', zipFileName, 'mORMot_2023-05-01', '*.pas;*.inc', {pmNew7Zip=}False, ProgressCallback);
  SaveFolderTo7Zip('c:\mORMot2\src\', zipFileName, 'mORMot_2023-05-02', '*.pas;*.inc', {pmNew7Zip=}False, ProgressCallback);
  SaveFolderTo7Zip('c:\mORMot2\src\', zipFileName, 'mORMot_2023-05-03', '*.pas;*.inc', {pmNew7Zip=}False, ProgressCallback);
  SaveFolderTo7Zip('c:\mORMot2\src\', zipFileName, 'mORMot_2023-05-04', '*.pas;*.inc', {pmNew7Zip=}False, ProgressCallback);
  SaveFolderTo7Zip('c:\mORMot2\src\', zipFileName, 'mORMot_2023-05-05', '*.pas;*.inc', {pmNew7Zip=}False, ProgressCallback);
end;

The result in TestMultiData.7z file looks as expected:

mORMot_2023-05-01\app
mORMot_2023-05-01\core
...
mORMot_2023-05-02\...
mORMot_2023-05-03\...
mORMot_2023-05-04\...
mORMot_2023-05-05\...

I have checked with a tool that compares directories, everything is OK for me.

With best regards
Thomas

Last edited by tbo (2023-05-06 21:04:43)

Offline

#4 2023-05-09 08:35:59

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,310
Website

Re: T7zWriter.SaveToFile, Mode error

Offline

#5 2023-05-09 20:16:55

tbo
Member
Registered: 2015-04-20
Posts: 337

Re: T7zWriter.SaveToFile, Mode error

Commit 5434 (e4a5ff6) works as expected. All my tests passed. Thanks for this.

With best regards
Thomas

Offline

Board footer

Powered by FluxBB