By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,279 Members | 1,670 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,279 IT Pros & Developers. It's quick & easy.

zip file corrupted over HTTP

P: 7
I have perl script which will make a zip file and create a HTML form with asubmit button to send the zip file client. When user click the download button the zip file will be downloaded. But the problem is now the downloaded zip file is corrupted.

here the scripts i wrote.

To create the Zip file.
Expand|Select|Wrap|Line Numbers
  1. #!/perl/bin/Perl.exe
  2.  
  3. use CGI;
  4.  
  5. use Archive::Zip qw( :ERROR_CODES );
  6.  
  7. print "Content-type: text/html \n\n";
  8.  
  9.  
  10.  
  11.  
  12. my $rme = new CGI;
  13. my $zip = Archive::Zip->new();
  14. my $file_name="";
  15. my $zipFile="";
  16.  
  17. #print "Searching file_name :: $file_name.....\n";
  18.  
  19. #To check whether the given file is available in the directory or not
  20.  
  21. opendir MYDIR, ".";
  22. @contents = readdir MYDIR;
  23.  
  24.  
  25. foreach(@contents)
  26. {
  27.  
  28. for($i = 0; $i < 140; ++$i)
  29. {
  30. #$zip->read("tst.zip") == AZ_OK or die "read error\n";
  31.  
  32.  
  33. $file_name="IOS_".$i.".xml";
  34. if($_ eq $file_name)
  35. {
  36. open(FILE1,$file_name);
  37. open(FILE,$file_name) or die "ERROR: Cannot Open file ($!)";
  38. while(<FILE>)
  39. {
  40.  
  41.  
  42. print FILE1 "$_";
  43. }
  44. $zip->addFile($file_name);
  45. unless ( $zip->writeToFileNamed('someFile.zip') == AZ_OK )
  46. {
  47. die 'write error';
  48. }
  49. print "************";
  50. last;
  51. }
  52. }
  53.  
  54. }
  55. #print ".$zip.".zip";
  56. print "<html><body>";
  57. print "<form action='/cgi-bin/filesave.pl'>";
  58. print "<select size='1' name='ID'>";
  59. print "<option value='someFile.zip'>someFile.zip</option> ";
  60. print "<option value='IOS_124.xml'>IOS_124.xml</option> ";
  61. print "</select>";
  62. print "<input type='submit' value='Submit' name='B1'>";
  63. print "</form></body></html>";
  64.  
To download the zip file
Expand|Select|Wrap|Line Numbers
  1. #!/perl/bin/Perl.exe
  2. #print "Content-type: text/html\n\n";
  3. use CGI ':standard';
  4. use CGI::Carp qw(fatalsToBrowser);
  5.  
  6. my $files_location;
  7. my $ID;
  8. my @fileholder;
  9. print "hello";
  10. #$files_location = "/cgi";
  11.  
  12. $ID = param('ID');
  13.  
  14. if ($ID eq '') {
  15.  
  16. print "You must specify a file to download.";
  17. } else {
  18.  
  19. open(DLFILE, "<$ID") || Error('open', 'file');
  20. @fileholder = <DLFILE>;
  21. close (DLFILE) || Error ('close', 'file');
  22.  
  23.  
  24.  
  25. print "Content-Type:application/x-octet-stream\n";
  26. print "Content-Disposition:attachment;filename=$ID\n\n";
  27. print "Content-Length;length <DLFILE>";
  28. print @fileholder
  29. }
Can anybody help me on this?
Mar 28 '08 #1
Share this Question
Share on Google+
4 Replies


KevinADC
Expert 2.5K+
P: 4,059
I have perl script which will make a zip file and create a HTML form with asubmit button to send the zip file client. When user click the download button the zip file will be downloaded. But the problem is now the downloaded zip file is corrupted.

here the scripts i wrote.

To create the Zip file.
#!/perl/bin/Perl.exe

use CGI;

use Archive::Zip qw( :ERROR_CODES );

print "Content-type: text/html \n\n";




my $rme = new CGI;
my $zip = Archive::Zip->new();
my $file_name="";
my $zipFile="";

#print "Searching file_name :: $file_name.....\n";

#To check whether the given file is available in the directory or not

opendir MYDIR, ".";
@contents = readdir MYDIR;


foreach(@contents)
{

for($i = 0; $i < 140; ++$i)
{
#$zip->read("tst.zip") == AZ_OK or die "read error\n";


$file_name="IOS_".$i.".xml";
if($_ eq $file_name)
{
open(FILE1,$file_name);
open(FILE,$file_name) or die "ERROR: Cannot Open file ($!)";
while(<FILE>)
{


print FILE1 "$_";
}
$zip->addFile($file_name);
unless ( $zip->writeToFileNamed('someFile.zip') == AZ_OK )
{
die 'write error';
}
print "************";
last;
}
}

}
#print ".$zip.".zip";
print "<html><body>";
print "<form action='/cgi-bin/filesave.pl'>";
print "<select size='1' name='ID'>";
print "<option value='someFile.zip'>someFile.zip</option> ";
print "<option value='IOS_124.xml'>IOS_124.xml</option> ";
print "</select>";
print "<input type='submit' value='Submit' name='B1'>";
print "</form></body></html>";

To download the zip file
#!/perl/bin/Perl.exe
#print "Content-type: text/html\n\n";
use CGI ':standard';
use CGI::Carp qw(fatalsToBrowser);

my $files_location;
my $ID;
my @fileholder;
print "hello";
#$files_location = "/cgi";

$ID = param('ID');

if ($ID eq '') {

print "You must specify a file to download.";
} else {

open(DLFILE, "<$ID") || Error('open', 'file');
@fileholder = <DLFILE>;
close (DLFILE) || Error ('close', 'file');



print "Content-Type:application/x-octet-stream\n";
print "Content-Disposition:attachment;filename=$ID\n\n";
print "Content-Length;length <DLFILE>";
print @fileholder
}
Can anybody help me on this?
See if this helps:

[code]$size = -s $ID;
open(DLFILE, "<$ID") || Error('open', 'file');
binmode DLFILE;
@fileholder = <DLFILE>;
close (DLFILE) || Error ('close', 'file');

Expand|Select|Wrap|Line Numbers
  1. print "Content-Type:application/x-download\n";
  2. print "Content-Length: $size\n";
  3. print "Content-Disposition:attachment;filename=$ID\n\n";
  4. print @fileholder
  5. }
Mar 28 '08 #2

P: 7
See if this helps:

$size = -s $ID;
open(DLFILE, "<$ID") || Error('open', 'file');
binmode DLFILE;
@fileholder = <DLFILE>;
close (DLFILE) || Error ('close', 'file');



print "Content-Type:application/x-download\n";
print "Content-Length: $size\n";
print "Content-Disposition:attachment;filename=$ID\n\n";
print @fileholder
}
Hi

Its working fine now .Thanks for your help.
Mar 28 '08 #3

P: 7
Hi,

Will this same script work in solaris plat form or we need to do some changes?
Apr 4 '08 #4

KevinADC
Expert 2.5K+
P: 4,059
Hi,

Will this same script work in solaris plat form or we need to do some changes?
If it does not work I guess you may need to make some changes, but I wouldn't know what changes.
Apr 4 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.