Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

bugfixes and reorg fs

  • Loading branch information...
commit 5ce84160a5654c28e5e2851e51fbbe84d3275695 1 parent 53ec0ed
@Vaporbook authored
View
113 OreillyZipListing.php
@@ -0,0 +1,113 @@
+<?php
+
+/*
+
+Originally used for zip bundles of Oreilly titles
+
+*/
+
+class OreillyZipListing {
+
+ public function __construct($z)
+ {
+ // construct with full path to zip file
+ $this->z = $z;
+ $this->files = array();
+ $this->handles = array();
+ $this->log("reading zip file ".$z);
+ $this->_open();
+ if(!is_resource($this->zh)) {
+ $this->log('error '.$this->zh);
+ exit;
+ } else {
+ while($e = zip_read($this->zh)) {
+ $this->files[] = array(
+ 'name'=>zip_entry_name($e),
+ 'size'=>zip_entry_filesize($e)
+ );
+ $this->handles[zip_entry_name($e)] = $e;
+ }
+ }
+ $this->_close();
+ }
+
+ public function getFiles()
+ {
+ return $this->files;
+ }
+
+ public function findFile($name, $linkonly=false)
+ {
+ $this->log('seeking '.$name);
+ $data = null;
+ foreach($this->files as $file) {
+ $basename = pathinfo($file['name'], PATHINFO_BASENAME);
+ $this->log($basename);
+ if($basename==$name) {
+ if($linkonly) {
+ $data = 'zip://' . $this->z . '#'.$file['name'];
+ } else {
+ $data = $this->getFile($file['name']);
+ }
+ break;
+ } else {
+ //echo "$basename==$name\n";
+ }
+ }
+ return $data;
+ }
+
+
+ public function getFile($name)
+ {
+ $this->_open();
+ $contents = '';
+
+ while($e = zip_read($this->zh)) {
+ if(zip_entry_name($e)==$name) {
+ if(zip_entry_open($this->zh, $e)) {
+ $size = zip_entry_filesize($e);
+ while($data = zip_entry_read($e,$size)) {
+ $contents .= $data;
+ }
+ }
+ }
+ }
+
+
+ /*
+ $e = $this->handles[$name];
+
+ if(zip_entry_open($this->zh, $e)) {
+ $size = zip_entry_filesize($e);
+ while($data = zip_entry_read($e,$size)) {
+ $contents .= $data;
+ }
+ } else {
+ $this->log('could not open');
+ }
+ */
+ $this->_close();
+ return $contents;
+ }
+
+ public function log($msg, $level=0)
+ {
+
+ //echo $msg . "\n";
+
+ }
+ private function _close()
+ {
+ zip_close($this->zh);
+ }
+ private function _open()
+ {
+ $this->zh = zip_open($this->z);
+ }
+
+
+
+
+
+}
View
BIN  epubs/c/Cory Doctorow - With a Little Help.epub
Binary file not shown
View
BIN  epubs/h/H. G. Wells - The War of the Worlds.epub
Binary file not shown
View
BIN  epubs/h/Harold MacGrath - Arms and the Woman.epub
Binary file not shown
View
BIN  epubs/j/Joseph Smith Fletcher - The Borough Treasurer.epub
Binary file not shown
View
BIN  epubs/k/Kris Ottman Neville - Earth Alert.epub
Binary file not shown
View
77 rename.php
@@ -0,0 +1,77 @@
+<?php
+
+require_once('./BookGluttonEpub.php');
+require_once('./BookGluttonZipEpub.php');
+
+/*
+
+Example and test script using the libraries to rename files
+according to metadata found in the epub file.
+
+
+*/
+
+$dir = realpath(dirname(__FILE__)).'/epubs/';
+$dir = $argv[1];
+
+$ite=new RecursiveDirectoryIterator($dir);
+
+$bytestotal=0;
+$nbfiles=0;
+
+foreach (new RecursiveIteratorIterator($ite) as $file=>$cur) {
+
+ if(!preg_match('/\.epub$/i',$file)) continue;
+
+ $filesize=$cur->getSize();
+ $bytestotal+=$filesize;
+ $nbfiles++;
+ echo "$file => $filesize\n";
+
+ try {
+
+ $epub = new BookGluttonZipEpub();
+ $epub->enableLogging();
+ $epub->loadZip($file);
+ $title = $epub->getTitle();
+ $author = $epub->getAuthor();
+ //$epub->close();
+
+ // how you do the actual rename is up to you -- our example
+ // just echoes what the operation will do:
+ $newtitle = preg_replace('/[\$\'\\\!\`\~\/\>\<\}\{\@\^\*]/',"","$author - $title".".epub");
+ echo "rename to ".$newtitle."\n";
+
+ if(!is_dir("$dir/$author")) {
+ mkdir("$dir/$author");
+ }
+
+ rename($file,"$dir/$author/$newtitle");
+
+ } catch (Exception $e) {
+
+ // BAD FILES go to bad file GHETTO
+
+ echo "Exception caught:".$e->getMessage()."\n----------\n";
+
+ rename($file,$dir.'/_GHETTO.'.$newtitle);
+
+ echo "Moved to ghetto.\n------------\n=============\n";
+
+
+ }
+
+}
+
+$bytestotal=number_format($bytestotal);
+echo "Total: $nbfiles files, $bytestotal bytes\n";
+
+exit();
+
+
+
+
+
+
+
+?>
Please sign in to comment.
Something went wrong with that request. Please try again.