Archive::Zip - is a member directory ?

M

MoshiachNow

Unzipping from a zip file,I need to establish if the member is a
directory or a file.
The following code does not seem to do it it for me,the result is
always ZERO (a file..).
Appreciate any advise:

$zip = Archive::Zip->new();
die if $zip->read( $filename1 ) != AZ_OK;
if (Archive::Zip::Member->isDirectory($NAME)) {
do something ...

I have also tried:
if ($zip->isDirectory($NAME)) {

$NAME passed can be directory or a file,and I expect "1" if it's a
directory.
The exit code from the above is always Zero ...Why ?
Surely something is wrong with my syntax.

Thanks
 
A

anno4000

MoshiachNow said:
Unzipping from a zip file,I need to establish if the member is a
directory or a file.
The following code does not seem to do it it for me,the result is
always ZERO (a file..).
Appreciate any advise:

$zip = Archive::Zip->new();
die if $zip->read( $filename1 ) != AZ_OK;
if (Archive::Zip::Member->isDirectory($NAME)) {
do something ...

Where did you get the idea isDirectory() could be called as a class
method? And how would it know about your specific archive $zip and
what is in it?

The way I understand the documentation, isDirectory() isn't a class method
but requires an Archive::Zip::Member object. You want to get one from
your archive. Untested:

if ( $zip->memberNamed( $NAME)->isDirectory ) {
# do something
}
I have also tried:
if ($zip->isDirectory($NAME)) {

You should consult the documentation instead of guessing.
$NAME passed can be directory or a file,and I expect "1" if it's a
directory.
The exit code from the above is always Zero ...Why ?

You didn't stick to the call conventions of isDirectory().
Surely something is wrong with my syntax.

The syntax is fine, otherwise it wouldn't compile. It's simply
wrong code.

Anno
 
M

MoshiachNow

The way I understand the documentation, isDirectory() isn't a class method
but requires an Archive::Zip::Member object. You want to get one from
your archive. Untested:

if ( $zip->memberNamed( $NAME)->isDirectory ) {
# do something
}

Thanks
I think it's fair to assume that the person asking a questing did read
the documentation to a certain extent ...
I have tried the above ,without any success:

Can't call method "isDirectory" on an undefined value at
script/rehost.pl line 2300.
BTH - $NAME IS defined in my script.

I think ,reading the doc,that object Archive::Zip::Member is only for
adding members,not for reading/identifying existing zip file members :

"Several constructors allow you to construct members without adding
them to a zip archive. These work the same as the addFile(),
addDirectory(), and addString() zip instance methods described above,
but they don't add the new members to a zip."

Does not say you can find info on existing members.
 
A

anno4000

MoshiachNow said:
Thanks
I think it's fair to assume that the person asking a questing did read
the documentation to a certain extent ...

I'll assume that unless the question proves the contrary.
I have tried the above ,without any success:

Can't call method "isDirectory" on an undefined value at
script/rehost.pl line 2300.

That shows that your archive doesn't have a member with name $NAME.
That's a bug in my code. Correction:

if ( my $member = $zip->memberNamed( $NAME) ) {
if ( $member->isDirectory ) {
# do something
} else {
# member exists but isn't a directory
}
} else {
# member doesn't exist
}
BTH - $NAME IS defined in my script.

It may be defined but apparently the archive doesn't have a member
of that name.
I think ,reading the doc,that object Archive::Zip::Member is only for
adding members,not for reading/identifying existing zip file members :

Archive::Zip::Member isn't an object, it's a class. Some of the methods
of this class, like isDirectory(), are definitely for identifying
existing members.
"Several constructors allow you to construct members without adding
them to a zip archive. These work the same as the addFile(),
addDirectory(), and addString() zip instance methods described above,
but they don't add the new members to a zip."

That is for construction of "free-floating" member objects that are
not in any zip archive. That's a special case that doesn't concern you.
Does not say you can find info on existing members.

Don't confuse the doc sections "Zip Archive Member Operations" with
"MEMBER OPERATIONS". The first one describes methods of the class
Archive::Zip that pertain to archive members. The second one is about
the methods of Archive::Zip::Member itself. Lots of these are for
finding info on existing members.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top