473,408 Members | 2,405 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

zip the folder

118 100+
hi,

i want to zip the folder using php. Any body plz help for that.
Aug 14 '08 #1
5 2750
pbmods
5,821 Expert 4TB
Heya, Swethak.

Check out PHP's zip functions (http://php.net/zip).
Aug 14 '08 #2
swethak
118 100+
hi,

i have the code for to create the zip folder.But in that i got the errors.plz tell that whats the mistake in my code.

createZip.inc.php

[php]

<?php
class createZip {
public $compressedData = array();
public $centralDirectory = array(); // central directory
public $endOfCentralDirectory = "\x50\x4b\x05\x06\x00\x00\x00\x00"; //end of Central directory record
public $oldOffset = 0;

public function addDirectory($directoryName) {
$directoryName = str_replace("\\", "/", $directoryName);

$feedArrayRow = "\x50\x4b\x03\x04";
$feedArrayRow .= "\x0a\x00";
$feedArrayRow .= "\x00\x00";
$feedArrayRow .= "\x00\x00";
$feedArrayRow .= "\x00\x00\x00\x00";

$feedArrayRow .= pack("V",0);
$feedArrayRow .= pack("V",0);
$feedArrayRow .= pack("V",0);
$feedArrayRow .= pack("v", strlen($directoryName) );
$feedArrayRow .= pack("v", 0 );
$feedArrayRow .= $directoryName;

$feedArrayRow .= pack("V",0);
$feedArrayRow .= pack("V",0);
$feedArrayRow .= pack("V",0);

$this -> compressedData[] = $feedArrayRow;

$newOffset = strlen(implode("", $this->compressedData));

$addCentralRecord = "\x50\x4b\x01\x02";
$addCentralRecord .="\x00\x00";
$addCentralRecord .="\x0a\x00";
$addCentralRecord .="\x00\x00";
$addCentralRecord .="\x00\x00";
$addCentralRecord .="\x00\x00\x00\x00";
$addCentralRecord .= pack("V",0);
$addCentralRecord .= pack("V",0);
$addCentralRecord .= pack("V",0);
$addCentralRecord .= pack("v", strlen($directoryName) );
$addCentralRecord .= pack("v", 0 );
$addCentralRecord .= pack("v", 0 );
$addCentralRecord .= pack("v", 0 );
$addCentralRecord .= pack("v", 0 );
$ext = "\x00\x00\x10\x00";
$ext = "\xff\xff\xff\xff";
$addCentralRecord .= pack("V", 16 );

$addCentralRecord .= pack("V", $this -> oldOffset );
$this -> oldOffset = $newOffset;

$addCentralRecord .= $directoryName;

$this -> centralDirectory[] = $addCentralRecord;
}

/**
* Function to add file(s) to the specified directory in the archive
*
* @param $directoryName string
*
*/

public function addFile($data, $directoryName) {

$directoryName = str_replace("\\", "/", $directoryName);

$feedArrayRow = "\x50\x4b\x03\x04";
$feedArrayRow .= "\x14\x00";
$feedArrayRow .= "\x00\x00";
$feedArrayRow .= "\x08\x00";
$feedArrayRow .= "\x00\x00\x00\x00";

$uncompressedLength = strlen($data);
$compression = crc32($data);
$gzCompressedData = gzcompress($data);
$gzCompressedData = substr( substr($gzCompressedData, 0, strlen($gzCompressedData) - 4), 2);
$compressedLength = strlen($gzCompressedData);
$feedArrayRow .= pack("V",$compression);
$feedArrayRow .= pack("V",$compressedLength);
$feedArrayRow .= pack("V",$uncompressedLength);
$feedArrayRow .= pack("v", strlen($directoryName) );
$feedArrayRow .= pack("v", 0 );
$feedArrayRow .= $directoryName;

$feedArrayRow .= $gzCompressedData;

$feedArrayRow .= pack("V",$compression);
$feedArrayRow .= pack("V",$compressedLength);
$feedArrayRow .= pack("V",$uncompressedLength);

$this -> compressedData[] = $feedArrayRow;

$newOffset = strlen(implode("", $this->compressedData));

$addCentralRecord = "\x50\x4b\x01\x02";
$addCentralRecord .="\x00\x00";
$addCentralRecord .="\x14\x00";
$addCentralRecord .="\x00\x00";
$addCentralRecord .="\x08\x00";
$addCentralRecord .="\x00\x00\x00\x00";
$addCentralRecord .= pack("V",$compression);
$addCentralRecord .= pack("V",$compressedLength);
$addCentralRecord .= pack("V",$uncompressedLength);
$addCentralRecord .= pack("v", strlen($directoryName) );
$addCentralRecord .= pack("v", 0 );
$addCentralRecord .= pack("v", 0 );
$addCentralRecord .= pack("v", 0 );
$addCentralRecord .= pack("v", 0 );
$addCentralRecord .= pack("V", 32 );

$addCentralRecord .= pack("V", $this -> oldOffset );
$this -> oldOffset = $newOffset;

$addCentralRecord .= $directoryName;

$this -> centralDirectory[] = $addCentralRecord;
}

/**
* Fucntion to return the zip file
*
* @return zipfile (archive)
*/

public function getZippedfile() {

$data = implode("", $this -> compressedData);
$controlDirectory = implode("", $this -> centralDirectory);

return
$data.
$controlDirectory.
$this -> endOfCentralDirectory.
pack("v", sizeof($this -> centralDirectory)).
pack("v", sizeof($this -> centralDirectory)).
pack("V", strlen($controlDirectory)).
pack("V", strlen($data)).
"\x00\x00";
}

/**
*
* Function to force the download of the archive as soon as it is created
*
* @param archiveName string - name of the created archive file
*/

public function forceDownload($archiveName) {
$headerInfo = '';

if(ini_get('zlib.output_compression')) {
ini_set('zlib.output_compression', 'Off');
}

// Security checks
if( $archiveName == "" ) {
echo "<html><title>Public Photo Directory - Download </title><body><BR><B>ERROR:</B> The download file was NOT SPECIFIED.</body></html>";
exit;
}
elseif ( ! file_exists( $archiveName ) ) {
echo "<html><title>Public Photo Directory - Download </title><body><BR><B>ERROR:</B> File not found.</body></html>";
exit;
}

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=".basename($archiveName).";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($archiveName));
readfile("$archiveName");

}

}

?>

[/php]

Zipfile.php

[php]

<?php
include_once("createZip.inc.php");
$createZip = new createZip;

$createZip -> addDirectory("dir/");

$fileContents = file_get_contents("img.jpg");
$createZip -> addFile($fileContents, "dir/img.jpg");
$fileName = "archive.zip";
$fd = fopen ($fileName, "wb");
$out = fwrite ($fd, $createZip -> getZippedfile());
fclose ($fd);

$createZip -> forceDownload($fileName);
@unlink($fileName);
?>
[/php]

error :

Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in F:\Facebook\furniture11\createZip.inc.php on line 3
Aug 17 '08 #3
Atli
5,058 Expert 4TB
Hi.

I've merged your new thread into this one, seeing as they are dealing with the same exact topic.
Try not to spread the same problem over multiple threads. It can get very messy very quickly.

As to the code.
Which version of PHP are you running?

The code you posted is clearly written for PHP5. The class visibility keywords are not supported by the old, deprecated, PHP4, nor any of it's predecessors, and would most likely cause parse errors, like the one you posted.

If you are still using PHP4, I strongly recommend upgrading to PHP5 as soon as possible.
Aug 17 '08 #4
swethak
118 100+
hi,

i have the code for to create the zip folder.But in that i got the errors.plz tell that whats the mistake in my code.

createZip.inc.php

[php]

<?php
class createZip {
public $compressedData = array();
public $centralDirectory = array(); // central directory
public $endOfCentralDirectory = "\x50\x4b\x05\x06\x00\x00\x00\x00"; //end of Central directory record
public $oldOffset = 0;

public function addDirectory($directoryName) {
$directoryName = str_replace("\\", "/", $directoryName);

$feedArrayRow = "\x50\x4b\x03\x04";
$feedArrayRow .= "\x0a\x00";
$feedArrayRow .= "\x00\x00";
$feedArrayRow .= "\x00\x00";
$feedArrayRow .= "\x00\x00\x00\x00";

$feedArrayRow .= pack("V",0);
$feedArrayRow .= pack("V",0);
$feedArrayRow .= pack("V",0);
$feedArrayRow .= pack("v", strlen($directoryName) );
$feedArrayRow .= pack("v", 0 );
$feedArrayRow .= $directoryName;

$feedArrayRow .= pack("V",0);
$feedArrayRow .= pack("V",0);
$feedArrayRow .= pack("V",0);

$this -> compressedData[] = $feedArrayRow;

$newOffset = strlen(implode("", $this->compressedData));

$addCentralRecord = "\x50\x4b\x01\x02";
$addCentralRecord .="\x00\x00";
$addCentralRecord .="\x0a\x00";
$addCentralRecord .="\x00\x00";
$addCentralRecord .="\x00\x00";
$addCentralRecord .="\x00\x00\x00\x00";
$addCentralRecord .= pack("V",0);
$addCentralRecord .= pack("V",0);
$addCentralRecord .= pack("V",0);
$addCentralRecord .= pack("v", strlen($directoryName) );
$addCentralRecord .= pack("v", 0 );
$addCentralRecord .= pack("v", 0 );
$addCentralRecord .= pack("v", 0 );
$addCentralRecord .= pack("v", 0 );
$ext = "\x00\x00\x10\x00";
$ext = "\xff\xff\xff\xff";
$addCentralRecord .= pack("V", 16 );

$addCentralRecord .= pack("V", $this -> oldOffset );
$this -> oldOffset = $newOffset;

$addCentralRecord .= $directoryName;

$this -> centralDirectory[] = $addCentralRecord;
}

/**
* Function to add file(s) to the specified directory in the archive
*
* @param $directoryName string
*
*/

public function addFile($data, $directoryName) {

$directoryName = str_replace("\\", "/", $directoryName);

$feedArrayRow = "\x50\x4b\x03\x04";
$feedArrayRow .= "\x14\x00";
$feedArrayRow .= "\x00\x00";
$feedArrayRow .= "\x08\x00";
$feedArrayRow .= "\x00\x00\x00\x00";

$uncompressedLength = strlen($data);
$compression = crc32($data);
$gzCompressedData = gzcompress($data);
$gzCompressedData = substr( substr($gzCompressedData, 0, strlen($gzCompressedData) - 4), 2);
$compressedLength = strlen($gzCompressedData);
$feedArrayRow .= pack("V",$compression);
$feedArrayRow .= pack("V",$compressedLength);
$feedArrayRow .= pack("V",$uncompressedLength);
$feedArrayRow .= pack("v", strlen($directoryName) );
$feedArrayRow .= pack("v", 0 );
$feedArrayRow .= $directoryName;

$feedArrayRow .= $gzCompressedData;

$feedArrayRow .= pack("V",$compression);
$feedArrayRow .= pack("V",$compressedLength);
$feedArrayRow .= pack("V",$uncompressedLength);

$this -> compressedData[] = $feedArrayRow;

$newOffset = strlen(implode("", $this->compressedData));

$addCentralRecord = "\x50\x4b\x01\x02";
$addCentralRecord .="\x00\x00";
$addCentralRecord .="\x14\x00";
$addCentralRecord .="\x00\x00";
$addCentralRecord .="\x08\x00";
$addCentralRecord .="\x00\x00\x00\x00";
$addCentralRecord .= pack("V",$compression);
$addCentralRecord .= pack("V",$compressedLength);
$addCentralRecord .= pack("V",$uncompressedLength);
$addCentralRecord .= pack("v", strlen($directoryName) );
$addCentralRecord .= pack("v", 0 );
$addCentralRecord .= pack("v", 0 );
$addCentralRecord .= pack("v", 0 );
$addCentralRecord .= pack("v", 0 );
$addCentralRecord .= pack("V", 32 );

$addCentralRecord .= pack("V", $this -> oldOffset );
$this -> oldOffset = $newOffset;

$addCentralRecord .= $directoryName;

$this -> centralDirectory[] = $addCentralRecord;
}

/**
* Fucntion to return the zip file
*
* @return zipfile (archive)
*/

public function getZippedfile() {

$data = implode("", $this -> compressedData);
$controlDirectory = implode("", $this -> centralDirectory);

return
$data.
$controlDirectory.
$this -> endOfCentralDirectory.
pack("v", sizeof($this -> centralDirectory)).
pack("v", sizeof($this -> centralDirectory)).
pack("V", strlen($controlDirectory)).
pack("V", strlen($data)).
"\x00\x00";
}

/**
*
* Function to force the download of the archive as soon as it is created
*
* @param archiveName string - name of the created archive file
*/

public function forceDownload($archiveName) {
$headerInfo = '';

if(ini_get('zlib.output_compression')) {
ini_set('zlib.output_compression', 'Off');
}

// Security checks
if( $archiveName == "" ) {
echo "<html><title>Public Photo Directory - Download </title><body><BR><B>ERROR:</B> The download file was NOT SPECIFIED.</body></html>";
exit;
}
elseif ( ! file_exists( $archiveName ) ) {
echo "<html><title>Public Photo Directory - Download </title><body><BR><B>ERROR:</B> File not found.</body></html>";
exit;
}

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=".basename($archiveName).";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($archiveName));
readfile("$archiveName");

}

}

?>

[/php]

Zipfile.php

[php]

<?php
include_once("createZip.inc.php");
$createZip = new createZip;

$createZip -> addDirectory("dir/");

$fileContents = file_get_contents("img.jpg");
$createZip -> addFile($fileContents, "dir/img.jpg");
$fileName = "archive.zip";
$fd = fopen ($fileName, "wb");
$out = fwrite ($fd, $createZip -> getZippedfile());
fclose ($fd);

$createZip -> forceDownload($fileName);
@unlink($fileName);
?>
[/php]

error :

Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in F:\Facebook\furniture11\createZip.inc.php on line 3
By using the php5 i solved the problem.In that only we created the zip file and created the new dir folder in that then after add the files into that folder.But my requirement is to convert the zip file from already existed folder.How i modify the above code.plz give the solution for that.
Aug 18 '08 #5
pbmods
5,821 Expert 4TB
That's pretty much how you have to do it. You create a new empty directory in the zip archive and then add the files.
Aug 18 '08 #6

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

Similar topics

13
by: jenny | last post by:
Hi, I am trying to find a VB way that would create a folder on all existing drives - the folder name would be the same on each drive. ie c:\backup, d:\backup, etc. But the folders would only be...
7
by: Tom | last post by:
Can anyone give me any advice on how to secure a folder on a network server so that documents in the folder can only be opened through an Access database or by the database admin. I need to store...
0
by: John H. | last post by:
In effort to understand (Outlook) MAPI folder tree structure wrote simple linear code below to navigate tree. Successive "...Folders.GetNext()"'s return same folder at all levels of tree. What...
4
by: Dave Veeneman | last post by:
I'm puzzling over the best design for a Folder object. I have two basic domain objects; leat's call them an Apple and an Orange. The objects are maintained in separate hierarchies, and each...
8
by: vinesh | last post by:
I have sample Asp.Net Web Application project. Let me know how to keep the files related to this project (like the webform.aspx, WebForm1.aspx.vb, WebForm1.aspx.resx) in a separate folder within a...
9
by: Paul | last post by:
I'm trying to make get my app to delete all the files in a specified folder and all the files within the folders of the specified folder. e.g. Folder 1 contains three files (File1, File2, File3)...
2
by: tatemononai | last post by:
Ok, this is a very unique bug. I have only been able to find one other post related to this online, and everybody who responded misunderstood the problem. I am not trying to reference anything in...
17
by: rdemyan via AccessMonster.com | last post by:
With A2003, I'm having trouble accessing files in a folder on another computer where back-end files, update files, etc are located. Here's the scenario: 1) Computer #1 - A2003 2) Computer #2 -...
24
by: biganthony via AccessMonster.com | last post by:
Hi, I have the following code to select a folder and then delete it. I keep getting a Path/File error on the line that deletes the actual folder. The line before that line deletes the files in...
5
by: Noozer | last post by:
I'm looking for a "smart folder" program to run on my Windows XP machine. I'm not having any luck finding it and think the logic behind the program is pretty simple, but I'm not sure how I'd...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.