Giter Club home page Giter Club logo

archive7z's Introduction

Wrapper 7-zip (p7zip)

License Latest Stable Version Continuous Integration

Features:

  • Supported all 7-zip formats:
    • Packing / unpacking: 7z, XZ, BZIP2, GZIP, TAR, ZIP and WIM.
    • Unpacking only: APFS, AR, ARJ, Base64, CAB, CHM, CPIO, CramFS, DMG, EXT, FAT, GPT, HFS, IHEX, ISO, LZH, LZMA, MBR, MSI, NSIS, NTFS, QCOW2, RAR, RPM, SquashFS, UDF, UEFI, VDI, VHD, VHDX, VMDK, XAR and Z.
  • List files and directories
  • Get contents of any file from archive
  • Delete files or directories
  • Add files or directories

Requirements:

Installation:

composer require gemorroj/archive7z

Notes:

Recommendations:

Archive7z focuses on wrapping up the original 7-zip features. But it is not always convenient to use in your application. Therefore, we recommend that you always create your own wrapper class over Archive7z with the addition of higher-level logic.

Example:

<?php
use Archive7z\Archive7z;

class MyArchive7z extends Archive7z
{
    protected $timeout = 120;
    protected $compressionLevel = 6;
    protected $overwriteMode = self::OVERWRITE_MODE_S;
    protected $outputDirectory = '/path/to/custom/output/directory';
}

$obj = new MyArchive7z('path_to_7z_file.7z');

if (!$obj->isValid()) {
    throw new \RuntimeException('Incorrect archive');
}

print_r($obj->getInfo());
/*
Archive7z\Info Object
(
    [path:Archive7z\Info:private] => /full_path_to/test.7z
    [type:Archive7z\Info:private] => 7z
    [physicalSize:Archive7z\Info:private] => 165343
    [headersSize:Archive7z\Info:private] => 241
    [method:Archive7z\Info:private] => LZMA2:192k
    [solid:Archive7z\Info:private] => +
    [blocks:Archive7z\Info:private] => 1
    [codePage:Archive7z\Info:private] => 
)
*/


// $obj->setPassword('123');

// $obj->getEntries('test', 100)
foreach ($obj->getEntries() as $entry) {
        print_r($entry);
/*
Archive7z\Entry Object
(
    [path:Archive7z\Entry:private] => 1.jpg
    [size:Archive7z\Entry:private] => 91216
    [packedSize:Archive7z\Entry:private] => 165344
    [modified:Archive7z\Entry:private] => 2013-06-10 09:56:07
    [created:Archive7z\Entry:private] => 
    [attributes:Archive7z\Entry:private] => A
    [crc:Archive7z\Entry:private] => 871345C2
    [encrypted:Archive7z\Entry:private] => +
    [method:Archive7z\Entry:private] => LZMA:192k 7zAES:19
    [block:Archive7z\Entry:private] => 0
    [comment:Archive7z\Entry:private] => 
    [hostOs:Archive7z\Entry:private] => 
    [characteristics:Archive7z\Entry:private] => 
    [folder:Archive7z\Entry:private] => 
    [archive:Archive7z\Entry:private] => MyArchive7z Object
        (
            [timeout:protected] => 60
            [compressionLevel:protected] => 6
            [overwriteMode:protected] => -aos
            [outputDirectory:protected] => /path/to/custom/output/directory
            [binary7z:Archive7z\Archive7z:private] => C:\Program Files\7-Zip\7z.exe
            [filename:Archive7z\Archive7z:private] => s:\VCS\Git\Archive7z\tests/fixtures/testPasswd.7z
            [password:Archive7z\Archive7z:private] => 123
            [encryptFilenames:protected] => 
        )
)
*/

    if ($entry->getPath() === 'test/test.txt') {
        $entry->extractTo('path_to_extract_folder/'); // extract the file
    }
}

echo $obj->getContent('test/test.txt'); // show content of the file
$obj->setOutputDirectory('path_to_extract_folder/')->extract(); // extract the archive
$obj->setOutputDirectory('path_to_extract_pass_folder/')->setPassword('pass')->extractEntry('test/test.txt'); // extract the password-protected entry

$solidMode = new SolidMode();
$solidMode->setMode(SolidMode::OFF);
$obj->setSolidMode($solidMode);
$obj->addEntry(__DIR__);  // add directory to the archive (include subfolders)
$obj->addEntry(__FILE__); // add file to the archive

$obj->renameEntry(__FILE__, __FILE__.'new'); // rename the file in the archive
$obj->delEntry(__FILE__.'new'); // remove the file from the archive

archive7z's People

Contributors

gemorroj avatar silverqx avatar wapmorgan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

archive7z's Issues

Hello, what is the problem causing this error?

Hello, what is the problem causing this error?

Binary of 7-zip is not available
root@:~# ls -l /usr/bin/7z
-rwxr-xr-x 1 www root 39 Aug 15  2020 /usr/bin/7z
root@:~# 7z -version

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,64 bits,1 CPU AMD EPYC 7601 32-Core Processor                 (800F12),ASM,AES-NI)

Debian11.6๏ผŒ php7.4.33

Need help with installation

Hi. Sorry for bothering you all, but I need some help with installing Archive7z.

My website runs on a 3rd party web hosting service. I uploaded all the files and it seems to be working fine except for one problem: It can't find the 7zip executable.

In Archive7zTrait.php, I changed the path specified in $binary7zNix, because there would be some problems with directory permissions, so I now just need the executable to upload. But I can't find a way to get it. I'm using Windows on my PC and I don't have access to the command line of the Linux server, that is used by the hosting service.

Could somebody please send me the executable, so I can upload it? Or if you think that this won't be enough, can you help me in any other way?

Question marks in Cyrillic file names

Hi all!

I have a problem with the Archive7z class. I made a function

public function unzip(string $pathToSource): DirectoryIterator
   {
       ...

      $obj = new Archive7z($pathToSource);
      $obj->setOutputDirectory($pathToDestination);
      $obj->extract();

      return new DirectoryIterator($pathToDestination);
   }

which returns a DirectoryIterator with the directory to unpack
files.

Next, I collect information about the unpacked files:

 $iterator = $this->zipper->unzip($zipFile->getRealPath());
 foreach ($iterator as $unzippedFile) {
     if (!in_array($unzippedFile->getFilename(), ['.', '..'])) {
         $encoding = mb_detect_encoding($unzippedFile->getFilename()); // ASCII
         $fileName = $unzippedFile->getFilename(); // 12_1_?????.txt
     }
  }

And on my server, the encoding is defined as ASCII, and in the file names instead of Cyrillic letters
question marks.

In the local environment(Docker), everything is displayed normally. mb_detect_encoding($unzippedFile->getFilename())
returns UTF-8 and the file names are correct.

I also tried to reproduce this error in docker and I managed to do it using the link https://zalinux.ru/?p=5740.
That is, I commented out the en_US.UTF-8 UTF-8 encoding in the PHP container in the /etc/locale.gen file and ran the command
locale-gen. After that, I only had ru_RU.UTF-8 UTF-8 encoding left. And after that the encoding of the unpacked files
also began to be defined as ASCII, not UTF-8, and question marks began to appear instead of Cyrillic characters ?

If we return the en_US.UTF-8 UTF-8 encoding in the container and execute locale-gen, then again everything works fine. Tell,
please, what can I do so that when unpacking files, Cyrillic characters are displayed in the file names, not signs
questions. How to make files unpacked in UTF-8 encoding and not ASCII?

getCrc() fails sometimes

For few archive types, CRC is not parsed from 7z l, so it is empty, so it fails with Exception

Fatal error: Uncaught TypeError: Archive7z\Entry::getCrc(): Return value must be of type string, null returned in /app/vendor/gemorroj/archive7z/src/Entry.php:240
Stack trace:
#0 /app/src/Drivers/SevenZip.php(217): Archive7z\Entry->getCrc()
#1 /app/src/UnifiedArchive.php(284): wapmorgan\UnifiedArchive\Drivers\SevenZip->getFileData('doc')
#2 /app/src/Commands/TableCommand.php(66): wapmorgan\UnifiedArchive\UnifiedArchive->getFileData('doc')

I found that crc is present for ZIP/TAR.GZ, and not present for TAR

getContent fails

Execution of example fails on this line:
echo $obj->getContent('test.txt');

Extracting tar.gz/tar.bz2 in one-step

Feature request: treat tar.gz/tgz archive as a tar archive with compression, not as an archive compressed to gz file.

Right now it treats UnifiedArchive.tar.gz like

                                          File name | Size | Date              
--------------------------------------------------------------------------------
UnifiedArchive.tar                                  | 0.0g |  01 Jan 1970, 3:01
--------------------------------------------------------------------------------
                                    Total 1 file(s) | 0.0g | 0.0g

My propose is to handle this case.
CLI for this is 7z x -so ../UnifiedArchive.tar.gz | 7z l -si -ttar.

Detailed information:
https://superuser.com/questions/80019/how-can-i-unzip-a-tar-gz-in-one-step-using-7-zip

Got $entry->getPackedSize() only for first file

Hi. Tried library and faced this issue.

                $this->seven_zip = new \Archive7z\Archive7z($filename);
                foreach ($this->seven_zip->getEntries() as $entry) {
                    var_dump($entry->getPackedSize());
                }

It returns only first file value. Also, that's value for all array, not for specified file.

string(5) "10584"
string(0) ""
string(0) ""
string(0) ""
string(0) ""
string(0) ""
string(0) ""
string(0) ""

Add inArchivePath in addEntry

Now addEntry($file, $includeSubFiles = false, $storePath = false) does not support setting name of file in archive. Do you plan to add functionality to specify in-archive path of file when adding it?

Reading previously divided archives

Hello and thank you for this cool tool!

I have a problem with reading contents of divided into parts archives. The command is failing, however showing some contents.

File names are stored as hash values.

Is there a simple solution to, maybe, ignore or fix this type of error without archive modification? The process is failing before I can catch it in an easy way.

Example output:

The command "'/usr/bin/7z' 'l' '/home/www/var/files/DB0/hash/f8/01/7c78e011b8ca53015b' '-slt' '-y' '-p '" failed.
Exit Code: 2(Misuse of shellbuiltins)
Working directory: /home/www

Output:                                                                                                                                                                        
================                                                                                                                                                               
7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov :2016-05-21
p7zip Version 16.02 (locale=C,Utf16=off,HugeFiles=on,64 bits,8 CPUs Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz (906E9),ASM,AES-NI)

Scanning the drive for archives:
1 file, 220200960 bytes (210 MiB)

Listing archive: /home/www/var/files/DB0/hash/f8/01/7c78e011b8ca53015b
--                                                                                                                                                                             
Path = /home/www/var/files/DB0/hash/f8/01/7c78e011b8ca53015b
Type = Rar
ERROR = Missing volume : f801b604de805be59b0b7d61288e3811ef31d6e9e091777c78e011b8ca53015b.r00
Physical Size = 220200960
Characteristics = Volume NewVolName FirstVolume VolCRC
Solid = -
Blocks = 1
Multivolume = +
Volume Index = 0
Volumes = 1
 ----------                                                                                                                                                                     
Path = setup_Full.exe
Folder = -
Size = 438531863
Packed Size = 220200861
Modified = 2008-09-09 12:52:34
Created =
Accessed =
Attributes = A
Encrypted = -
Solid = -
Commented = -
Split Before = -
Split After = +
CRC = 1B0C199D
Host OS = Win32
Method = m0:22
Version = 20

Errors: 1

physicalSize missing from gzip files

getinfo() on gzip not returning much data, specially physicalSize

[path:Archive7z\Info:private] => /test.tar.gz
[type:Archive7z\Info:private] => gzip
[physicalSize:Archive7z\Info:private] =>
[headersSize:Archive7z\Info:private] => 25
[method:Archive7z\Info:private] =>
[solid:Archive7z\Info:private] =>
[blocks:Archive7z\Info:private] =>
[codePage:Archive7z\Info:private] =>

To do the equivalent of / and \ in file paths

To unify the directory separators for different file systems.
For example:

$obj = new Archive7z('test.7z');
$obj->getEntry('test/test.txt'); // must be equivalent
$obj->getEntry('test\\test.txt'); // must be equivalent

Failed on macos

\Archive7z\Archive7z::getAutoCli on OSX returns cliWindows, because PHP_OS contains 'Darwin'
So, I think it will be good to fix it and add support for 7z on Mac (e.g. keka archiver https://github.com/aonez/Keka)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.