Error(unexpected end of file)-while extracting zip file created using new ZipArchive

Discussion in 'PHP' started by priyahdarsini, Mar 15, 2009.

  1. #1
    Hi

    My requirement is need to zip an uploaded file and send as an attachment through mail. At the receipient end the mail will be opened and the file will be extracted.

    I used the below code to create zip file. It is working fine when .txt files are added to zip. I am able to do both zip and extract.

    But when I am adding .doc files to zip I am able to create .zip file with no issues. When extracting below error occurs. I am using winzip for extraction after saving the .zip to my local machine. This issue occurs only if I try it in online. I am using windows server. When I try the same in my local machine it is working fine.

    Please Suggest

    Error
    ----
    Extracting to "C:\Documents and Settings\Administrator\Local Settings\Temp\"
    Use Path: no Overlay Files: yes
    Extracting testing.doc
    Error: unexpected end of file encountered
    Error: invalid compressed data to inflate

    Code used for creating .zip file
    -----------------------------

    <?
    $path="d:/consultant_detail/";

    if ($_FILES["resumepath"]["name"] != "")
    {
    $resumepath=$_FILES["resumepath"]["name"];
    move_uploaded_file($_FILES["resumepath"]["tmp_name"],$path.$resumepath);
    }

    $zip = new ZipArchive();
    $filename = "data_test.zip";

    if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE)
    exit("cannot open <$filename>\r\n");

    $zip->addFile($path.$resumepath,"test.doc");
    $zip->close();
    ?>


    Please, any one suggest to clear this issue.
     
    priyahdarsini, Mar 15, 2009 IP
  2. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #2
    How large is your zip file? It is 0 bytes? What does the table of contents look like?
     
    SmallPotatoes, Mar 16, 2009 IP
  3. priyahdarsini

    priyahdarsini Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    My .zip file is not 0 bytes.

    If I add two files (.txt file and word document) to zip I am able to open the .txt file. But when opening .doc file the error occurs.

    Please refer the screen shot attached in that I showed the process as step1,2 and 3. By ignoring all errors if I extract that .zip file in that I am able to see only the .txt file
     

    Attached Files:

    priyahdarsini, Mar 16, 2009 IP
  4. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Sorry, it's hard to see the screen shot - it's tiny!
     
    SmallPotatoes, Mar 16, 2009 IP
  5. priyahdarsini

    priyahdarsini Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hi

    I attached the created .zip file herewith. I am using windows server and php 5.2.0
     

    Attached Files:

    priyahdarsini, Mar 19, 2009 IP
  6. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #6
    How are you retrieving the zip file? I wonder if maybe it's getting garbled in the process of moving it from your server to you. ASCII FTP? Antivirus? Virus?

    Your code seems fine, and clearly it has done its best to add the files - they show in the TOC:

      Length     Date   Time    Name
     --------    ----   ----    ----
          307  03-17-09 02:23   Shanthini_detail.txt
        33280  03-13-09 18:41   test.doc
     --------                   -------
        33587                   2 files
    Code (markup):
    It's been at least 10 years since I was masochistic enough to try to do any work using a Windows server, so unfortunately I'm not very familiar with any platform-specific issues that may arise. Somebody else might be able to help along that vein.
     
    SmallPotatoes, Mar 19, 2009 IP
  7. priyahdarsini

    priyahdarsini Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hi

    Thanks for your help.

    I tried as per 'http://www.phpconcept.net/pclzip/man/en/index.php?methods-add' and it is working fine without PECL extensions.
     
    priyahdarsini, Mar 21, 2009 IP
  8. priyahdarsini

    priyahdarsini Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hi

    I am creating zip file using the below reference link.

    'http://www.phpconcept.net/pclzip/man/en/index.php?methods-add'


    I am able to create zip files correctly. But 'PCLZIP_OPT_REMOVE_PATH' is not working. I am not able to remove extra path :mad:

    Below is the code I am using. I am using windows server and php 5.2.0


    <?
    $cur_day="26";
    $subfolder="shanthini";
    $txt_filename1="test1.txt";
    $txt_filename2="test2.txt";

    require_once("../pclzip.lib.php");

    $filename = $cur_day."/".$subfolder."/data_".$subfolder.".zip";

    $archive = new PclZip($filename);

    $file1=$cur_day."/".$subfolder."/".$txt_filename1;
    $file2=$cur_day."/".$subfolder."/".$txt_filename2;

    if ($resumepath != "")
    $v_list = $archive->add($file1.",".$file2,PCLZIP_OPT_REMOVE_PATH,$removepath);
    else
    $v_list = $archive->add($file1,PCLZIP_OPT_REMOVE_PATH,$removepath);

    if ($v_list == 0)
    {
    $action="error";
    }
    ?>


    can any one suggest
     
    priyahdarsini, Mar 26, 2009 IP
  9. priyahdarsini

    priyahdarsini Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Hi

    Thanks for your help.

    I tried as per 'http://www.phpconcept.net/pclzip/man/en/index.php?methods-add' and it is working fine without PECL extensions.
     
    Last edited: Aug 3, 2009
    priyahdarsini, Aug 3, 2009 IP
  10. priyahdarsini

    priyahdarsini Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Again I am heving trouble

    It shows javascript error for the below code can any one suggest

    <?
    $rows=$_POST['h_rows'];
    $cols=$_POST['h_cols'];

    $display_date_e=explode("-",$_POST['display_date']);
    $display_date=$display_date_e[2]."-".$display_date_e[0]."-".$display_date_e[1];

    $name=$_POST['name'];

    //echo "insert into grid set name='$name', rows='$rows', cols='$cols', display_date='$display_date'<br>";

    if ($_GET['id']=="")
    {
    mysql_query("insert into grid set name='$name', rows='$rows', cols='$cols', display_date='$display_date'");
    $ins_id=mysql_insert_id();
    }
    else
    {
    mysql_query("update grid set name='$name', rows='$rows', cols='$cols', display_date='$display_date'");
    $ins_id=$_GET['id'];
    }

    $cellv="";
    for ($i=1; $i<=$rows; $i++)
    {
    $rowvs="";
    $rowv="";

    for ($j=1; $j<=$rows; $j++)
    {
    $box_n="box_".$i."_".$j;
    $boxs_n="boxs_".$i."_".$j;

    $cellv=$_POST[$box_n];
    $rowv.=$cellv.",";

    $cellvs=$_POST[$boxs_n];
    $rowvs.=$cellvs.",";
    }
    //echo "update grid set r".$i."='$rowv', r".$i."s='$rowvs' where id='$ins_id'<br>";
    mysql_query("update grid set r".$i."='$rowv', r".$i."s='$rowvs' where id='$ins_id'");
    }

    ?>

    -----------------

    <?

    $rows=3;
    $cols=3;

    $size="width=25 height=45";
    if ($_POST['rows']!="") $rows=$_POST['rows'];
    if($_POST['cols']!="") $cols=$_POST['cols'];

    $but_caption="Add";
    if ($_GET['id'] !="")
    {
    $id=$_GET['id'];
    $but_caption="Update";
    }

    $ftch_values=mysql_fetch_object(mysql_query("select * from grid where id='$id'"));

    $display_date_e=explode("-",$ftch_values->display_date);
    $display_date=$display_date_e[1]."-".$display_date_e[2]."-".$display_date_e[0];

    if ($ftch_values->rows!="") $rows=$ftch_values->rows;
    if($ftch_values->cols!="") $cols=$ftch_values->cols;

    ?>
    <script>
    function frm_validate()
    {
    var msg_start="Required Fields\n";
    var msg="";
    var flg="";

    var box_chk="false";
    var boxs_chk="true";

    if (document.getElementById("name").value=="")
    {
    msg+="Name\n";

    if (flg=="")
    flg=document.getElementById("name");
    }

    if (document.getElementById("display_date").value=="")
    {
    msg+="Display Date\n";

    if (flg=="")
    flg=document.getElementById("display_date");
    }

    var rows=document.getElementById("h_rows").value;
    var cols=document.getElementById("h_cols").value;

    for (i=1; i<rows; i++)
    {
    for (j=1; j<cols; j++)
    {
    if (document.getElementById("box_"+i+"_"+j).value!="" && box_chk=='false') box_chk="true";
    if (document.getElementById("boxs_"+i+"_"+j).value=="") boxs_chk="false";
    }
    }

    if (box_chk=="false")
    {
    msg+="Puzzle\n";

    if (flg=="")
    flg=document.getElementById("box_1_1");
    }

    if (boxs_chk=="false")
    {
    msg+="Solution\n";

    if (flg=="")
    flg=document.getElementById("boxs_1_1");
    }


    if (msg!="")
    {
    alert(msg_start+msg);
    flg.focus();
    return false;
    }
    }

    function show_solution()
    {
    var r,c;
    var val_o;

    var rows=document.getElementById("h_rows").value;
    var cols=document.getElementById("h_cols").value;

    if (document.getElementById("show_soln").checked==true)
    document.getElementById("solution_grid").style.display='';
    if (document.getElementById("show_soln").checked==false)
    document.getElementById("solution_grid").style.display='none';

    for (r=1; r<=rows; r++)
    {
    for (c=1; c<=cols; c++)
    {
    val_o=document.getElementById("box_"+r+"_"+c).value;

    document.getElementById("boxs_"+r+"_"+c).value=val_o;
    }

    }

    }
    </script>
    <script language="javascript" type="text/javascript" src="datetimepicker.js"></script>
    <style>
    .text_box
    {
    border:1px #ffffff solid
    }
    </style>
    <tr><td>

    <table>
    <tr>
    <td valign=top>

    <table>
    <form name="frm_grid1" action="grid.php" method="post">
    <tr><td>No. of rows</td><td>
    <select name="rows" id="rows">
    <?for ($i=3; $i<=10; $i++){?>
    <option value="<?=$i?>"><?=$i?></option>
    <?}?>
    </select>
    <?if ($rows>0){?>
    <script>
    document.getElementById("rows").value="<?=$rows?>";
    </script>
    <?}?>
    </td></tr>
    <tr><td>No. of columns</td><td>
    <select name="cols" id="cols">
    <?for ($i=3; $i<=10; $i++){?>
    <option value="<?=$i?>"><?=$i?></option>
    <?}?>
    </select>
    <?if ($cols>0){?>
    <script>
    document.getElementById("cols").value="<?=$cols?>";
    </script>
    <?}?>
    <input type=submit name=submit1 id=submit1 value=Submit>
    </form>

    <form name="frm_grid2" action="grid_action.php?id=<?=$_GET['id']?>" method="post">
    <input type=hidden name="h_rows" id="h_rows" value="<?=$rows?>">
    <input type=hidden name="h_cols" id="h_cols" value="<?=$cols?>">
    </td></tr>
    <tr><td>Name</td><td><input type=text name=name id=name value="<?=$ftch_values->name?>"></td></tr>
    <tr><td>Display Date</td><td><input type=text name=display_date id=display_date value="<?=$display_date?>">
    <a href="javascript:NewCal('display_date','ddmmyyyy')">
    <img src="images/cal.gif" width="16" height="16" border="0" alt="Pick a date">
    </a>
    </td></tr>
    <tr><td colspan=2>
    Puzzle
    <table border="1" width="250" cellspacing=0 cellpadding=0>
    <?for ($ri=1; $ri<=$rows; $ri++)
    {?>
    <tr>
    <?for ($ci=1; $ci<=$cols; $ci++){
    ?>
    <td align=center width=15 height=40 bgcolor="<?=$bgcolor?>">
    <?
    $r="r".$ri;
    $r_e=explode(",",$ftch_values->$r);
    ?>
    <input type=text name="box_<?=$ri?>_<?=$ci?>" value="<?=$r_e[$ci-1];?>" size=1 class=text_box>
    </td>
    <?}?>
    </tr>
    <?}?>
    </table>
    </td></tr>
    <tr><td colspan=2>
    <input type=checkbox onclick="javascript:show_solution()" id=show_soln name=show_soln>Click to enter solution
    </td></tr>
    <tr><td id="solution_grid" style="display:none" colspan=2>
    Solution
    <table border="1" width="250" cellspacing=0 cellpadding=0>
    <?for ($ri=1; $ri<=$rows; $ri++)
    {?>
    <tr>
    <?for ($ci=1; $ci<=$cols; $ci++){
    ?>
    <td align=center width=15 height=40 bgcolor="<?=$bgcolor?>">
    <?
    $rs="r".$ri."s";
    $rs_e=explode(",",$ftch_values->$rs);
    ?>
    <input type=text name="boxs_<?=$ri?>_<?=$ci?>" size=1 class=text_box value="<?=$rs_e[$ci-1];?>">

    </td>
    <?}?>
    </tr>
    <?}?>
    </table>
    </td></tr>
    <tr><td colspan=2><input type=submit name=submit id=submit value="<?=$but_caption?>" onclick="javascript:return frm_validate()"></td></tr>
    </form>
    </table>
    </td></tr>
    <?include("bottom.php")?>

    <?if ($_GET['id']!=""){?>
    <script>
    document.getElementById("solution_grid").style.display='';
    </script>
    <?}?>

    &nbsp;

    ----------
     
    priyahdarsini, Aug 4, 2009 IP