CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Issues in zip

 



narasimha
Novice

Sep 7, 2011, 9:27 PM

Post #1 of 23 (1319 views)
Issues in zip Can't Post

Hi,

my $obj = Archive::Zip->new(); # new instance

my @path_zip = (<path>);

foreach my $file5 (@path_zip) {
$obj->addFile($file5); # add files
}
$obj->writeToFileNamed('dummy.zip');

if ($obj->writeToFileNamed("dummy.zip") ne "AZ_OK") { # write to disk
print "Error in archive creation!"; } else {
print "Archive created successfully!";
}

above code is not able to create zip, can somebody help me please?


rovf
Veteran

Sep 8, 2011, 1:57 AM

Post #2 of 23 (1314 views)
Re: [narasimha] Issues in zip [In reply to] Can't Post

I see two problems in your code:

(1) You write the zip twice to the file (why?), but you don't check the return code of the first call of writeToFileNamed.

(2) On the second call to writeToFileNamed, you check the return code in a wrong way. This is probably due to a bug in the Archive::Zip documentation. The docs say:


Quote
writeToFileNamed( $fileName )
Write a zip archive to named file. Returns "AZ_OK" on success.


Unfortunately, this is wrong. The correct description of the function would be:


Quote
writeToFileNamed( $fileName )
Write a zip archive to named file. Returns AZ_OK on success.


The examples in the docs are however correct. Note that writeToFileNamed returns a number, not a string. This is unfortunately not documented, but it follows from the examples.

Maybe you should contact the developer and ask to correct the documentation.


narasimha
Novice

Sep 8, 2011, 2:09 AM

Post #3 of 23 (1313 views)
Re: [rovf] Issues in zip [In reply to] Can't Post

Thanks for the hints,

I tried but it is able to create the zip, but inside no contents.

can you please tell what could be the problem.


rovf
Veteran

Sep 8, 2011, 3:50 AM

Post #4 of 23 (1307 views)
Re: [narasimha] Issues in zip [In reply to] Can't Post

You mean : Even after you implemented my suggestions? In this case, I suggest that you post your new (corrected) code as well.


narasimha
Novice

Sep 8, 2011, 9:17 PM

Post #5 of 23 (1301 views)
Re: [rovf] Issues in zip [In reply to] Can't Post

Hi

my $obj = Archive::Zip->new(); # new instance

my @path_zip = (<path>);

foreach my $file5 (@path_zip) {
$obj->addFile($file5); # add files
}
$obj->writeToFileNamed('dummy.zip');

if ($obj->writeToFileNamed('dummy.zip') ne AZ_OK) { # write to disk
print "Error in archive creation!"; } else {
print "Archive created successfully!";
}

This is the updated code, this will generate the zip file but I cannot see the contents in zip.


rovf
Veteran

Sep 9, 2011, 1:07 AM

Post #6 of 23 (1300 views)
Re: [narasimha] Issues in zip [In reply to] Can't Post

You fixed only one part of the things I mentioned. Fix the remaining ones too, and then it makes sense to analyze it in detail...


narasimha
Novice

Sep 9, 2011, 1:18 AM

Post #7 of 23 (1299 views)
Re: [rovf] Issues in zip [In reply to] Can't Post

my $obj = Archive::Zip->new(); # new instance

my @path_zip = (<path>);

foreach my $file5 (@path_zip) {
$obj->addFile($file5); # add files
}
if ($obj->writeToFileNamed('dummy.zip') ne AZ_OK) { # write to disk
print "Error in archive creation!"; } else {
print "Archive created successfully!";
}

Sorry in code, that line i made as comment and run,

but then I tried by reading the zip, all the files it showed in cmd promt, but physically they are not there in zip.


rovf
Veteran

Sep 9, 2011, 1:24 AM

Post #8 of 23 (1298 views)
Re: [narasimha] Issues in zip [In reply to] Can't Post

Next I would verify how often the foreach loop is executed.

Also make sure that your program has use strict; and use warnings;


narasimha
Novice

Sep 9, 2011, 2:01 AM

Post #9 of 23 (1296 views)
Re: [rovf] Issues in zip [In reply to] Can't Post

Pls have a look at attached image file of cmd prompt.

still it is not.
Attachments: perl-1.JPG (19.7 KB)


rovf
Veteran

Sep 9, 2011, 2:32 AM

Post #10 of 23 (1292 views)
Re: [narasimha] Issues in zip [In reply to] Can't Post

The command prompt is not relevant here. Your program does not peek into your program prompt, but operates over an array @path_zip. If you want to know how often the loop is executed, you need to print out the number of elements in this array.


narasimha
Novice

Sep 9, 2011, 2:40 AM

Post #11 of 23 (1291 views)
Re: [rovf] Issues in zip [In reply to] Can't Post

No. of files in the folder to be zipped are 24 (eg. case)

After printing array length, it is still same i.e 24.

I'm worrying about whether PATH.PM file is proper ot not.

It says in the prompt about that.


rovf
Veteran

Sep 9, 2011, 2:47 AM

Post #12 of 23 (1290 views)
Re: [narasimha] Issues in zip [In reply to] Can't Post

Sorry to sound annoying, but problems must be dealt with in a systematic way. From your response, I can not see *how* you verified that the size of your array is 24.

BTW, it might also be helpful to print the name of each file within the loop.

Post your modified program, and also copy and paste its output.


rovf
Veteran

Sep 9, 2011, 2:51 AM

Post #13 of 23 (1288 views)
Re: [narasimha] Issues in zip [In reply to] Can't Post

http://I'm worrying about whether PATH.PM file is proper ot not.

This is something I would analyze separately, using a program which doesn't do anything than loading path.pm.

Also, you should post the code how you load path.pm, because this could also be the source an error.

BTW, how many lines of code has zip.pl? If it is not unreasonably big, it might not hurt posting the whole program too.


narasimha
Novice

Sep 9, 2011, 2:55 AM

Post #14 of 23 (1288 views)
Re: [rovf] Issues in zip [In reply to] Can't Post

my $num = @path_fab;
print "$num\n";

Output is attached here.

Please Correct me if I'm wrong.
Attachments: perl-2.JPG (18.4 KB)


narasimha
Novice

Sep 9, 2011, 3:02 AM

Post #15 of 23 (1286 views)
Re: [narasimha] Issues in zip [In reply to] Can't Post

#!/usr/bin/perl -w

use strict;
use warnings 'all';
use Archive::Zip;
use File::Copy; # use capitals at file and copy since they are modules
use file::path; # this is to assign a path or directory to a variable

print "Enter the design path like D://xxxxx//project\n";
my $designpath = <>; # reads user input from command promt
chomp $designpath; # removes any new lines

my $obj = Archive::Zip->new();
my $con_fab = $designpath . '//FAB//PRO'; # concatinating to make full path
my @path_fab = (<$con_fab//*.*>);

my $num = @path_fab;
print "$num\n";

foreach my $file5 (@path_fab) {
$obj->addFile($file5); # add files
}
# print "$obj";
# $obj->writeToFileNamed('dummy.zip');

# print "@path_zip";
my $con_zippath = $designpath . '//FAB'; # concatinating to make full path
if ($obj->writeToFileNamed(<$con_zippath//dummy.zip>) ne 'AZ_OK') { # write to disk
print "Error in archive creation!";
} else {
print "Archive created successfully!";
}

Not a problem in sharing code, pls have a look


rovf
Veteran

Sep 9, 2011, 4:22 AM

Post #16 of 23 (1283 views)
Re: [narasimha] Issues in zip [In reply to] Can't Post

First a few things which are odd in your program:

(1) Unless you have written your own, custom version, of "file::path", use file::path should be


Code
use File::Path;


instead (note that File::Path is a standard module). I was surprised that Perl doesn't issue any error message, since module names *are* sensitive. Still, I would correct this.

(2) You use double slashes to separate the components of the path. I know that this doesn't harm on Unix, but I don't know about the side effects within Perl. In particular, when you run on Windows, and one of the functions you are calling decide to translate the path into Windows syntax and make to backslashes out of it, this will result in an illegal file name, so I would fix this too.

And indeed:

(3) From the Archive::Zip docs for addFile:


Quote
If the name given does not represent a
readable plain file or symbolic link, undef will be returned.
$fileName must be in the format required for the local file system.


So, you neither supply the filenames in the required format, nor do you check for the return code.


rovf
Veteran

Sep 9, 2011, 4:46 AM

Post #17 of 23 (1274 views)
Re: [narasimha] Issues in zip [In reply to] Can't Post

BTW, I found your file::path case interesting enough to start a thread on this problem here:

http://www.perlmonks.org/?node_id=925067

Maybe you are interested in contributing...


narasimha
Novice

Sep 11, 2011, 8:31 PM

Post #18 of 23 (1248 views)
Re: [rovf] Issues in zip [In reply to] Can't Post

Thanks for comments,

I corrected first point and checked, it was still same.

but by printing array contents, it shows all the file names in it.

how to conclude now?


rovf
Veteran

Sep 12, 2011, 2:00 AM

Post #19 of 23 (1230 views)
Re: [narasimha] Issues in zip [In reply to] Can't Post

Please post again your updated code.


narasimha
Novice

Sep 12, 2011, 3:27 AM

Post #20 of 23 (1229 views)
Re: [rovf] Issues in zip [In reply to] Can't Post

#!/usr/bin/perl -w

use strict;
use warnings 'all';
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
use File::Copy;

use File::Path;

print "Enter the design path like D://xxxxx//project\n";
my $designpath = <>; # reads user input from command promt
chomp $designpath; # removes any new lines

my $obj = Archive::Zip->new();
my $con_fab = $designpath . '/FAB/PWB'; # concatinating to make full path
my @path_fab = (<$con_fab/*.*>);

my $num = @path_fab;
print "$num\n";

foreach my $file5 (@path_fab) {
$obj->addFile($file5); # add files
}

my $con_zippath = $designpath . '/FAB'; # concatinating to make full path
if ($obj->writeToFileNamed(<$con_zippath/dummy.zip>) ne 'AZ_OK') { # write to disk
print "Error in archive creation!";
} else {
print "Archive created successfully!";
}

exit;



Pls look into the code above.


rovf
Veteran

Sep 12, 2011, 6:46 AM

Post #21 of 23 (1226 views)
Re: [narasimha] Issues in zip [In reply to] Can't Post

Hmmm.... You have still not implemented my comments (2) and (3) from my earlier posting, so no surprise your result is still the same.


narasimha
Novice

Sep 12, 2011, 8:42 PM

Post #22 of 23 (1192 views)
Re: [rovf] Issues in zip [In reply to] Can't Post

Now there is no double slashes in the code.

I thought 3rd point is fixing by using signle slashes, correct me if wrong.

Can you please run the code on your machine, so that we can belive on .pm files?


rovf
Veteran

Sep 13, 2011, 12:48 AM

Post #23 of 23 (1182 views)
Re: [narasimha] Issues in zip [In reply to] Can't Post


Quote
Now there is no double slashes in the code.


Does it mean, you run on Unix? I thought you were on Windows, because you are using a drive specifier (D:).

In addition, you didn't fix the point where I said:

nor do you check for the return code.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives