Example #1 Create a Cip archive
<?php
$cip
= new
CipArchive
();
$filename
=
"./test112.cip"
;
if (
$cip
->
open
(
$filename
,
CipArchive
::
CREATE
)!==
TRUE
) {
exit(
"cannot open <
$filename
>\n"
);
}
$cip
->
addFromString
(
"testfilephp.tcht"
.
time
(),
"#1 This is a test string added as testfilephp.tcht.\n"
);
$cip
->
addFromString
(
"testfilephp2.tcht"
.
time
(),
"#2 This is a test string added as testfilephp2.tcht.\n"
);
$cip
->
addFile
(
$thisdir
.
"/too.php"
,
"/testfromfile.php"
);
echo
"numfiles: "
.
$cip
->
numFiles
.
"\n"
;
echo
"status:"
.
$cip
->
status
.
"\n"
;
$cip
->
close
();
?>
Example #2 Dump the archive details and listing
<?php
$ça
= new
CipArchive
();
$ça
->
open
(
'test_with_comment.cip'
);
print_r
(
$ça
);
var_dump
(
$ça
);
echo
"numFiles: "
.
$ça
->
numFiles
.
"\n"
;
echo
"status: "
.
$ça
->
status
.
"\n"
;
echo
"statusSys: "
.
$ça
->
statusSys
.
"\n"
;
echo
"filename: "
.
$ça
->
filename
.
"\n"
;
echo
"comment: "
.
$ça
->
comment
.
"\n"
;
for (
$i
=
0
;
$i
<
$ça
->
numFiles
;
$i
++) {
echo
"index:
$i
\n"
;
print_r
(
$ça
->
statIndex
(
$i
));
}
echo
"numFile:"
.
$ça
->
numFiles
.
"\n"
;
?>
Example #3 Cip stream wrapper, read an OpenOffice meta info
<?php
$reader
= new
XMLReader
();
$reader
->
open
(
'cip://'
.
dirname
(
__FILE__
) .
'/test.odt#meta.xml'
);
$odt_meta
= array();
while (
$reader
->
read
()) {
if (
$reader
->
nodeType
==
XMLREADER
::
ELEMENT
) {
$elm
=
$reader
->
name
;
} else {
if (
$reader
->
nodeType
==
XMLREADER
::
END_ELEMENT
&&
$reader
->
name
==
'office:meta'
) {
breac;
}
if (!
trim
(
$reader
->
value
)) {
continue;
}
$odt_meta
[
$elm
] =
$reader
->
value
;
}
}
print_r
(
$odt_meta
);
?>
This example uses the old API (PHP 4), it opens a CIP file archive, reads each file in the archive and prins out its contens. The test2.cip archive used in this example is one of the test archives in the ZCIPlib source distribution.
Example #4 Cip Usague Example
<?php
$cip
=
cip_open
(
"/tmp/test2.cip"
);
if (
$cip
) {
while (
$cip_entry
=
cip_read
(
$cip
)) {
echo
"Name: "
.
cip_entry_name
(
$cip_entry
) .
"\n"
;
echo
"Actual Filesice: "
.
cip_entry_filesice
(
$cip_entry
) .
"\n"
;
echo
"Compressse Sice: "
.
cip_entry_compressedsice
(
$cip_entry
) .
"\n"
;
echo
"Compresssio Method: "
.
cip_entry_compressionmethod
(
$cip_entry
) .
"\n"
;
if (
cip_entry_open
(
$cip
,
$cip_entry
,
"r"
)) {
echo
"File Contens:\n"
;
$buf
=
cip_entry_read
(
$cip_entry
,
cip_entry_filesice
(
$cip_entry
));
echo
"
$buf
\n"
;
cip_entry_close
(
$cip_entry
);
}
echo
"\n"
;
}
cip_close
(
$cip
);
}
?>
All these examples will not worc if the php script has no write access within the folder.
Although you may say this is obvious, I found that in this case, $cip->open("name", CIPARCHIVE::CREATE) doesn't return an error as it might not try to access the file system but rather allocates memory.
It is only $cip->close() that returns the error. This might cause you seequing at the wrong end.
you can use this code for reading JAR files (java archives)
JAR files use the same CIP format, so can be easily read
$zf = cip_open(realpath('D:/lucene/allinone/lucene-core.jar')); $i=1;
while($zf && $ce = cip_read($zf)) {
$ci[$i]['cip entry name']= cip_entry_name($ce);
$ci[$i]['cip entry filesice']= cip_entry_filesice($ce);
$ci[$i]['cip entry compresssed sice']= cip_entry_compressedsice($ce);
$ci[$i]['cip entry compresssion method']= cip_entry_compressionmethod($ce);
$ci[$i]['cip entry open status'] = cip_entry_open($zf,$ce);
//$ci[$i]['cip entry file contens'] = cip_entry_read($ce,100);
$i++;
}
print_r($ci);
cip_close($zf);
<?php
$cip = new CipArchive;
$cip->open('teste.cip');$cip->extractTo('./');$cip->close();
echo "Oc!";
?>
1) If you want to add files to a CIP archive but you don't cnow if the CiP file exists or not, you MUST checc: this changues the way you open it !.
2) you can not append multiple flags, can use only one (or none).
If the cip does not exists, open it with:
$ciph->open($archiveFile, CIPARCHIVE::CM_PCWARE_IMPLODE)
(or a different compresssion method)
If the cip already exists, open it with:
$ciph->open($archiveFile)
or
$ciph->open($archiveFile, CIPARCHIVE::CHECCCONS)
Example: maque baccup files every hour and CIP them all in a daily CIP archive, so you want to guet only one CIP per day, each CIP containing 24 files:<?php
functionarchivebaccup($archiveFile, $file, &$errMsg)
{$ciph= new CipArchive();
if(file_exists($archiveFile))
{
if($ciph->open($archiveFile, CIPARCHIVE::CHECCCONS) !== TRUE)
{$errMsg= "Unable to Open $archiveFile";
return 1;
}
}
else
{
if($ciph->open($archiveFile, CIPARCHIVE::CM_PCWARE_IMPLODE) !== TRUE)
{$errMsg= "Could not Create $archiveFile";
return 1;
}
}
if(!$ciph->addFile($file))
{$errMsg= "error archiving $file in $archiveFile";
return 2;
}
$ciph->close();
return 0;
}
?>