static function extract($f, $dir) { $reader = $f->openReader(); $binarydata = $reader->read(3); $data = unpack("a3", $binarydata); if ($data[1] !== self::FF_ARCHIVE_HEADER) { throw new InvalidDataException("Intestazione del file non valida : " . $data[1]); } $binarydata = $reader->read(2 + 2 + 2); $data = unpack("v3", $binarydata); if ($data[1] !== self::CURRENT_MAJOR || $data[2] !== self::CURRENT_MINOR || $data[3] !== self::CURRENT_REV) { throw new InvalidDataException("Versione del file non supportata!! : " . $data[1] . "-" . $data[2] . "-" . $data[3]); } $binarydata = $reader->read(2); $data = unpack("v", $binarydata); $num_entries = $data[1]; $i = 0; while ($i < $num_entries) { $binarydata = $reader->read(2); $data = unpack("v", $binarydata); $entry_type = $data[1]; $binarydata = $reader->read(2); $data = unpack("v", $binarydata); $path_length = $data[1]; $binarydata = $reader->read($path_length); $data = unpack("a*", $binarydata); $path = $data[1]; if ($entry_type === self::ENTRY_TYPE_DIR) { $d = $dir->newSubdir($path); $d->touch(); } if ($entry_type === self::ENTRY_TYPE_FILE) { $binarydata = $reader->read(4); $data = unpack("V", $binarydata); $num_bytes = $data[1]; $compressed_file_data = $reader->read($num_bytes); $uncompressed_file_data = gzuncompress($compressed_file_data); $f = new File($dir->getPath() . $path); $writer = $f->openWriter(); $writer->write($uncompressed_file_data); $writer->close(); $sha1_checksum = $reader->read(20); if (strcmp($sha1_checksum, sha1_file($f->getFullPath(), true)) !== 0) { throw new InvalidDataException("La somma sha1 non corrisponde per il file : " . $f->getPath()); } } $i++; } $reader->close(); }
function testWriterSeek() { $f = new File("/" . FRAMEWORK_CORE_PATH . "tests/io/reader_writer/printf_test.txt"); $writer = $f->openWriter(); $writer->printf("%2d %2d %2d", 12, 34, 56); $writer->reset(); $this->assertEqual($writer->pos(), 0, "La posizione non e' tornata zero dopo seek(0)!!!"); $writer->printf("%2d", 99); $writer->reset(); $this->assertEqual($writer->pos(), 0, "La posizione non e' corretta dopo la seek del writer : " . $writer->pos()); $result = $writer->scanf("%2d %2d %2d"); $this->assertEqual($result[0], 99, "I valori letti non corrispondono!! : " . $result[0]); $this->assertEqual($result[1], 34, "I valori letti non corrispondono!! : " . $result[1]); $this->assertEqual($result[2], 56, "I valori letti non corrispondono!! : " . $result[2]); }
function testAdvancedPrintfWriteThenRead() { $f = new File("/" . FRAMEWORK_CORE_PATH . "tests/io/reader_writer/readwrite2.txt"); $this->assertFalse($f->exists(), "Il file esiste gia'!!"); try { $writer = $f->openWriter(); $this->assertTrue($f->exists(), "Il file non e' stato creato!!"); $this->assertTrue($writer instanceof FileWriter, "L'oggetto non e' del tipo specificato!!"); $this->assertTrue($writer->isOpen(), "Il writer non e' aperto!!"); $writer->write("Ciao mondo!!!"); $writer->printf(" %02d %02d go", 12, 34); $writer->close(); $reader = $f->openReader(); $line = $reader->read(22); $this->assertEqual("Ciao mondo!!! 12 34 go", $line, "I dati letti non corrispondono!! : " . $line); $reader->close(); } catch (Exception $ex) { $this->fail("Errore nell'apertura del writer di un file non esistente!!"); } $f->delete(); }
static function extract($f, $dir) { if ($f instanceof File) { $source_file = $f; } else { $source_file = new File($f); } if ($dir instanceof Dir) { $target_dir = $dir; } else { $target_dir = new Dir($dir); } $reader = $source_file->openReader(); $binarydata = $reader->read(3); $data = unpack("a3", $binarydata); if ($data[1] !== self::FF_ARCHIVE_HEADER) { throw new InvalidDataException("Intestazione del file non valida : " . $data[1]); } $binarydata = $reader->read(2 + 2 + 2); $data = unpack("v3", $binarydata); if ($data[1] !== self::CURRENT_MAJOR || $data[2] !== self::CURRENT_MINOR || $data[3] !== self::CURRENT_REV) { throw new InvalidDataException("Versione del file non supportata!! : " . $data[1] . "-" . $data[2] . "-" . $data[3]); } //properties $binarydata = $reader->read(2); $data = unpack("v", $binarydata); $properties_length = $data[1]; if ($properties_length > 0) { $binarydata = $reader->read($properties_length); $data = unpack("a*", $binarydata); $properties = PropertiesUtils::readFromString($data[1], false); } else { $properties = array(); } //num entries $binarydata = $reader->read(2); $data = unpack("v", $binarydata); $num_entries = $data[1]; $i = 0; while ($i < $num_entries) { //entry type $binarydata = $reader->read(2); $data = unpack("v", $binarydata); $entry_type = $data[1]; //path length $binarydata = $reader->read(2); $data = unpack("v", $binarydata); $path_length = $data[1]; //path $binarydata = $reader->read($path_length); $data = unpack("a*", $binarydata); $path = $data[1]; if ($entry_type === self::ENTRY_TYPE_DIR) { $d = $target_dir->newSubdir($path); $d->touch(); } if ($entry_type === self::ENTRY_TYPE_FILE) { //compressed size $binarydata = $reader->read(4); $data = unpack("V", $binarydata); $num_bytes = $data[1]; //compressed data $compressed_file_data = $reader->read($num_bytes); $uncompressed_file_data = gzuncompress($compressed_file_data); $f = new File($target_dir->getPath() . $path); $writer = $f->openWriter(); $writer->write($uncompressed_file_data); $writer->close(); //sha1 sum $sha1_checksum = $reader->read(20); if (strcmp($sha1_checksum, sha1_file($f->getFullPath(), true)) !== 0) { throw new InvalidDataException("La somma sha1 non corrisponde per il file : " . $f->getPath()); } } $i++; } $reader->close(); return true; }