Zipping files with the same name inside a folder

Asked

Viewed 639 times

0

I would like to know if there is the possibility of the following scenario being done in Delhpi:

I have a briefcase and inside it I have the following structure:

foto.jpg
foto.png
imagem.jpg
imagem.png

is it possible to zip, by name? so... zip only the files that contain the same name...:

foto.zip -> contem -> foto.jpg e foto.png
imagem.zip -> contem -> imagem.jpg e imagem.png

I do not want a ready answer, but rather the ability to do this in Delphi, remembering that the files I may not know their name.

2 answers

0


You can use the class System.zip.Tzipfile from Delphi itself to do this implementation by looping the files with the same name (with different extensions) and adding them to the zip as per the rule you set, follow the example based on the question:

uses
  System.SysUtils,
  StrUtils,
  Classes,
  System.Zip;

var
  vArquivos: TStringList;
  vDiretorio: String;
  vDiretorioSaida: String;
  searchResult : TSearchRec;
  vArquivoSemExtensao: String;
  I: Integer;
  ZipFile: TZipFile;
begin
  vDiretorio := 'D:\Arquivos';
  vDiretorioSaida := 'D:\ArquivosZipado';
  vArquivos := TStringList.Create;
  try
    if findfirst(vDiretorio+'\*.*', faAnyFile, searchResult) = 0 then
    begin
      repeat
        vArquivos.Sort;
        if (searchResult.Name <> '.') and (searchResult.Name <> '..') then
        begin
          vArquivoSemExtensao := ChangeFileExt(searchResult.Name, '');
          if not vArquivos.Find(vArquivoSemExtensao, I) Then
            vArquivos.Add(vArquivoSemExtensao);
        end;
      until FindNext(searchResult) <> 0;
    end;
    FindClose(searchResult);

    for I := 0 to vArquivos.Count -1 do
    begin
      ZipFile := TZipFile.Create;
      try
        ZipFile.Open(vDiretorioSaida+'\'+vArquivos.Strings[I]+'.zip', zmWrite);
        if findfirst(vDiretorio+'\'+vArquivos.Strings[I]+'.*', faAnyFile, searchResult) = 0 then
        begin
          repeat
            ZipFile.add(vDiretorio+'\'+searchResult.Name);
          until (FindNext(searchResult) <> 0);
        end;
        FindClose(searchResult);
      finally
        FreeAndNil(ZipFile);
      end;
    end;
  finally
    FreeAndNil(vArquivos);
  end;
end.
  • Especially, only one question... instead of vDirectory+'*. * accept every type of file...it would be possible to accept only one type? for example only png and jpg.. I tried to pass .jpg|.png.. but he does not recognize.

  • I did a test here and it doesn’t really let pass more than one extension in findfirst, an option would be inside the repeat of the file search using the Extractfileext function passing as parameter the searchResult.Name, so you can know the file extension and make the tractive you want.

-1

Yes it is possible, in case I used Winrar but with zip it works too

"C:\Program Files (x86)\WinRAR\WinRAR.exe" a "E:\Backup\foto.rar" "E:\Backup\foto.*" 

If you use "*" instead of the extension, all files with the name "photo" independent of the extension will be compressed...

If you have any questions you can take a look here

  • interesting approach, but I may not know the name of the file that is in the folder

  • But how will you compress the files by name then? You would need to know the extension or the name of the files at least

  • exact, by name... I thought of something like derrun the folder and locate everyone who has the same name and yes zip..

  • Then, you just have to go through the folder, saving each file name (the repeated ones you record only one), then you take and execute the method I told you for each of the names recorded. As you said, I did not give a ready answer, but rather a part of it... You can, by Delphi, scroll through the files in the folder and get the name of each one of them

Browser other questions tagged

You are not signed in. Login or sign up in order to post.