Adding files to a tar file

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Adding files to a tar file

Luciano de Souza
Hello all,

This code should add all the files of a directory in a tar file.

program e01;
{$mode objfpc}{$H+}

uses
libtar, sysutils;

var
archive: TTarWriter;
search: TSearchRec;

BEGIN
archive := TTarWriter.create('arquivo.tar');
with archive do
begin
try
FindFirst('/home/luciano/Documentos/*', faAnyFile, search);
repeat
if (search.name <> '.') and (search.name <> '..') then
AddFile(search.name);
until FindNext(Search) = 0;
finally
free;
end;
end;
END.

The program compiles, but I got an error. The message says that the
first file of the directory can't be opened.

What is wrong?

My second doubt is: I am running this program in Ubuntu 13.10. If I
run it in Windows, will a external library be necessary or all the
code is included in lib tar unit?

Regards,



--
Luciano de Souza
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Adding files to a tar file

etrusco
On Tue, Jan 21, 2014 at 10:45 PM, luciano de souza <[hidden email]> wrote:

> Hello all,
>
> This code should add all the files of a directory in a tar file.
>
> program e01;
> {$mode objfpc}{$H+}
>
> uses
> libtar, sysutils;
>
> var
> archive: TTarWriter;
> search: TSearchRec;
>
> BEGIN
> archive := TTarWriter.create('arquivo.tar');
> with archive do
> begin
> try
> FindFirst('/home/luciano/Documentos/*', faAnyFile, search);
> repeat
> if (search.name <> '.') and (search.name <> '..') then
> AddFile(search.name);
> until FindNext(Search) = 0;
> finally
> free;
> end;
> end;
> END.
>
> The program compiles, but I got an error. The message says that the
> first file of the directory can't be opened.
>
> What is wrong?

Probably because Search.Name contains only the filename, but AddFile
expects an absolute path (or relative to GetCurrentDir)?


> My second doubt is: I am running this program in Ubuntu 13.10. If I
> run it in Windows, will a external library be necessary or all the
> code is included in lib tar unit?
>
> Regards,

AFAICS all the code is included in the libtar unit.

-Flávio
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Adding files to a tar file

Luciano de Souza
Yes, that's the mistake. I forgot the fact "search.name" does not
returns a full path. This code works very well.
program e01;
{$mode objfpc}{$H+}

uses
libtar, sysutils;

var
archive: TTarWriter;
search: TSearchRec;
path: string;

BEGIN
archive := TTarWriter.create('arquivo.tar');
with archive do
begin
try
path := paramstr(1);
FindFirst(path + '/*', faAnyFile, search);
repeat
if (search.name <> '.') and (search.name <> '..') then
AddFile(path + '/' + search.name);
until FindNext(Search) = 0;
finally
free;
end;
end;
END.

Thank you! This class is really very easy to use.

2014/1/21, Flávio Etrusco <[hidden email]>:

> On Tue, Jan 21, 2014 at 10:45 PM, luciano de souza <[hidden email]>
> wrote:
>> Hello all,
>>
>> This code should add all the files of a directory in a tar file.
>>
>> program e01;
>> {$mode objfpc}{$H+}
>>
>> uses
>> libtar, sysutils;
>>
>> var
>> archive: TTarWriter;
>> search: TSearchRec;
>>
>> BEGIN
>> archive := TTarWriter.create('arquivo.tar');
>> with archive do
>> begin
>> try
>> FindFirst('/home/luciano/Documentos/*', faAnyFile, search);
>> repeat
>> if (search.name <> '.') and (search.name <> '..') then
>> AddFile(search.name);
>> until FindNext(Search) = 0;
>> finally
>> free;
>> end;
>> end;
>> END.
>>
>> The program compiles, but I got an error. The message says that the
>> first file of the directory can't be opened.
>>
>> What is wrong?
>
> Probably because Search.Name contains only the filename, but AddFile
> expects an absolute path (or relative to GetCurrentDir)?
>
>
>> My second doubt is: I am running this program in Ubuntu 13.10. If I
>> run it in Windows, will a external library be necessary or all the
>> code is included in lib tar unit?
>>
>> Regards,
>
> AFAICS all the code is included in the libtar unit.
>
> -Flávio
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>


--
Luciano de Souza
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Adding files to a tar file

wkitty42
In reply to this post by Luciano de Souza
On 1/21/2014 7:45 PM, luciano de souza wrote:
> The program compiles, but I got an error. The message says that the
> first file of the directory can't be opened.
>
> What is wrong?

your code uses faAnyfile... perhaps you need to be more restrictive? is your
problem caused by a sub-directory with the directory of files you are trying to
archive? or perhaps there's a soft/hard-link?

i also do not see a findclose call after you finish with findfirst/findnext...
granted, it may not be needed since you are exiting the program but it is more
proper, IMHO...


--
NOTE: No off-list assistance is given without prior approval.
       Please keep mailing list traffic on the list unless
       private contact is specifically requested and granted.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal