📄 zip.php
字号:
return $before - $this->offset;
}
/**
* @see File_Archive_Reader::tell()
*/
function tell()
{
return $this->offset;
}
function uncompressData()
{
if ($this->data !== null)
return;
$this->data = $this->source->getData($this->header['CLen']);
if (PEAR::isError($this->data)) {
return $this->data;
}
if ($this->header['Method'] == 8) {
$this->data = gzinflate($this->data);
}
if ($this->header['Method'] == 12) {
$this->data = bzdecompress($this->data);
}
if (crc32($this->data) != $this->header['CRC']) {
return PEAR::raiseError("Zip archive: CRC fails on entry ".
$this->currentFilename);
}
}
/**
* @see File_Archive_Reader::makeWriterRemoveFiles()
*/
function makeWriterRemoveFiles($pred)
{
require_once "File/Archive/Writer/Zip.php";
$blocks = array();
$seek = null;
$gap = 0;
if ($this->currentFilename !== null && $pred->isTrue($this)) {
$seek = 30 + $this->header['File'] + $this->header['Extra'] + $this->header['CLen'];
$blocks[] = $seek; //Remove this file
array_pop($this->files);
}
while (($error = $this->nextWithFolders()) === true) {
$size = 30 + $this->header['File'] + $this->header['Extra'] + $this->header['CLen'];
if (substr($this->getFilename(), -1) == '/' || $pred->isTrue($this)) {
array_pop($this->files);
if ($seek === null) {
$seek = $size;
$blocks[] = $size;
} else if ($gap > 0) {
$blocks[] = $gap; //Don't remove the files between the gap
$blocks[] = $size;
$seek += $size;
} else {
$blocks[count($blocks)-1] += $size; //Also remove this file
$seek += $size;
}
$gap = 0;
} else {
if ($seek !== null) {
$seek += $size;
$gap += $size;
}
}
}
if (PEAR::isError($error)) {
return $error;
}
if ($seek === null) {
$seek = 4;
} else {
$seek += 4;
if ($gap == 0) {
array_pop($blocks);
} else {
$blocks[] = $gap;
}
}
$writer = new File_Archive_Writer_Zip(null,
$this->source->makeWriterRemoveBlocks($blocks, -$seek)
);
if (PEAR::isError($writer)) {
return $writer;
}
foreach ($this->files as $file) {
$writer->alreadyWrittenFile($file['name'], $file['stat'], $file['CRC'], $file['CLen']);
}
$this->close();
return $writer;
}
/**
* @see File_Archive_Reader::makeWriterRemoveBlocks()
*/
function makeWriterRemoveBlocks($blocks, $seek = 0)
{
if ($this->currentFilename === null) {
return PEAR::raiseError('No file selected');
}
$keep = false;
$this->uncompressData();
$newData = substr($this->data, 0, $this->offset + $seek);
$this->data = substr($this->data, $this->offset + $seek);
foreach ($blocks as $length) {
if ($keep) {
$newData .= substr($this->data, 0, $length);
}
$this->data = substr($this->data, $length);
$keep = !$keep;
}
if ($keep) {
$newData .= $this->data;
}
$filename = $this->currentFilename;
$stat = $this->currentStat;
$writer = $this->makeWriterRemove();
if (PEAR::isError($writer)) {
return $writer;
}
unset($stat[7]);
$stat[9] = $stat['mtime'] = time();
$writer->newFile($filename, $stat);
$writer->writeData($newData);
return $writer;
}
/**
* @see File_Archive_Reader::makeAppendWriter
*/
function makeAppendWriter()
{
require_once "File/Archive/Writer/Zip.php";
while (($error = $this->next()) === true) { }
if (PEAR::isError($error)) {
$this->close();
return $error;
}
$writer = new File_Archive_Writer_Zip(null,
$this->source->makeWriterRemoveBlocks(array(), -4)
);
foreach ($this->files as $file) {
$writer->alreadyWrittenFile($file['name'], $file['stat'], $file['CRC'], $file['CLen']);
}
$this->close();
return $writer;
}
/**
* This function seeks to the start of the [end of central directory] field,
* just after the \x50\x4b\x05\x06 signature and returns the number of bytes
* skipped
*
* The stream must initially be positioned before the end of central directory
*/
function seekToEndOfCentralDirectory()
{
$nbSkipped = $this->source->skip();
$nbSkipped -= $this->source->rewind(22) - 4;
if ($this->source->getData(4) == "\x50\x4b\x05\x06") {
return $nbSkipped;
}
while ($nbSkipped > 0) {
$nbRewind = $this->source->rewind(min(100, $nbSkipped));
while ($nbRewind >= -4) {
if ($nbRewind-- && $this->source->getData(1) == "\x50" &&
$nbRewind-- && $this->source->getData(1) == "\x4b" &&
$nbRewind-- && $this->source->getData(1) == "\x05" &&
$nbRewind-- && $this->source->getData(1) == "\x06") {
//We finally found it!
return $nbSkipped - $nbRewind;
}
}
$nbSkipped -= $nbRewind;
}
return PEAR::raiseError('End of central directory not found. The file is probably not a zip archive');
}
/**
* This function will fill the central directory variable
* and seek back to where it was called
*/
function readCentralDirectory()
{
$nbSkipped = $this->seekToEndOfCentralDirectory();
if (PEAR::isError($nbSkipped)) {
return $nbSkipped;
}
$this->source->skip(12);
$offset = $this->source->getData(4);
$nbSkipped += 16;
if (PEAR::isError($offset)) {
return $offset;
}
$offset = unpack("Vvalue", $offset);
$offset = $offset['value'];
$current = $this->source->tell();
$nbSkipped -= $this->source->rewind($current - $offset);
//Now we are the right pos to read the central directory
$this->centralDirectory = array();
while ($this->source->getData(4) == "\x50\x4b\x01\x02") {
$this->source->skip(12);
$header = $this->source->getData(16);
$nbSkipped += 32;
if (PEAR::isError($header)) {
return $header;
}
$header = unpack('VCRC/VCLen/VNLen/vFileLength/vExtraLength', $header);
$this->centralDirectory[] = array('CRC' => $header['CRC'],
'CLen' => $header['CLen'],
'NLen' => $header['NLen']);
$nbSkipped += $this->source->skip(14 + $header['FileLength'] + $header['ExtraLength']);
}
$this->source->rewind($nbSkipped+4);
}
}
?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -