How to Search for Files and Zip Them in One Zip File

how can i search for files and zip them in one zip file

The command you use will run zip on each file separately, try this:

find . -name <name> -print | zip newZipFile.zip -@

The -@ tells zip to read files from the input. From man zip(1),

-@ file lists. If a file list is specified as -@ [Not on MacOS], zip takes the list of input files from standard input instead of from the command line.

How to search for specific zip file from the directory?

You can use glob to search for files using wildcards.

import os
import glob
import datetime

top = r"C:\Users\jezequiel\Desktop"

d = datetime.date(1998, 8, 21)
ds = d.strftime('%Y%m%d')
pattern = f'*_{ds}_salary.zip' # use wildcards (*)

# make sure we have a match
with open(f'foobar_{ds}_salary.zip', 'wb') as f:
pass

print(glob.glob(os.path.join(top, pattern)))

How to search a string inside a zip file inside other zip file if any

Hi
Your question was nice for me and i think about it one day and write the code for solving this problem in general :)

What we want to do?

We want to have an iterator that give a path and modifier and itrate the all files in the path recursively and modify the some specific file and we said to it

What is the solution?

We make a function exec that get the path and change the file that we mention in our modifier and if it see a directory in the path is send its data to the exec again (call is recursively) and is see the zip file extracted it and name it in special way (because when you want to zip this extracted file again you need to know that was a extracted directory and you need to know the original name too) and again go throw it

        $files = $this->fileManager->getFileNamesInPath($path);
foreach ($files as $fileName) {
$extension = $this->nameManager->getFileExtentionFromFileName($fileName);

if ($this->nameManager->isZipFile($extension)) {
$tempExtractedDirectoryName = $this->nameManager->getTempExtractedDirectoryName($fileName);
$this->fileManager->unzip($path, $fileName ,$tempExtractedDirectoryName);
$this->exec($this->nameManager->getAbsolutePathWithDirectory($path, $tempExtractedDirectoryName));
}

if ($this->nameManager->isDirectory($extension)) {
$this->exec($this->nameManager->getAbsolutePathWithDirectory($path, $fileName));
}

foreach ($this->modifiers as $modifierExtention => $modifierFunction) {
if ($extension == $modifierExtention) {
$modifierFunction($this->nameManager->getAbsolutePathWithDirectory($path, $fileName));
}
}
}

After that we check the directory name and if we are in the extracted directory we zip it and replace it with the old zip file and then remove the old zip file

        $folderName = $this->nameManager->getCurrectDirectoryNameFromAbsolutePath($path);
if ($this->isExtentedDirectory($folderName)) {
$zipFileName = $this->getZipFileNameFromExtendedDirectoryName($folderName);
$zipFilePath = $this->getZipFilePath($path, $folderName);

$this->fileManager->remove($zipFilePath . $zipFileName);
$this->fileManager->zip($zipFilePath, $zipFileName, $folderName);
$this->fileManager->remove($path);
}

So with this work exec function iterate all files in the path recursively and do what you want in the files that you say in your modifier

what is the modifier

You can do what you want in the files with modifier

For example is you want to add one line to the test file you should to sth like this and use the addModifier function:

$path = getcwd();
$object = new PhpDirectoryIterator();

$object->addModifier("txt", function ($path) {
$myfile = fopen($path, "a") or die("Unable to open file!");
$txt = "test\n";
fwrite($myfile, $txt);
fclose($myfile);
}
);

$object->exec($path);

emample

.
├── a.txt
├── b.txt
├── main.php
├── test
│ └── c.txt
└── test.zip

They are my files

a.txt , b.txt and c.txt are empty text files

And there is a d.txt in the test.zip which is a empty text file

And that is main.php file

<?php

class PhpFileManager
{
public function unzip($dir, $zipFileNamem ,$destonationName)
{
exec("cd $dir && unzip $zipFileNamem -d $destonationName");
}

public function remove($path)
{
exec("rm -rf $path");
}

public function zip($path, $zipFileName ,$foldername)
{
exec("cd $path$foldername && zip -r ../$zipFileName .");
}

public function getFileNamesInPath($path)
{
return array_diff(scandir($path), ['.','..']);
}
}

class PhpNameManager
{
const ZIP_EXTENTION = 'zip';
const ZIP_EXTENTION_WITH_DOT = '.zip';
const DITECTORY_EXTENTION = '';
const HASH_ALGORITHM = 'md5';

public function getFileExtentionFromFileName($fileName)
{
$explodedFileName = explode('.', $fileName);
if (count($explodedFileName) == 1) {
return '';
}

return end($explodedFileName);
}

public function getTempExtractedDirectoryName($fileName)
{
return $fileName . $this->hash($fileName);
}

public function hash($text)
{
return hash(self::HASH_ALGORITHM, $text);
}

public function getAbsolutePathWithDirectory($path, $directory)
{
return $path . '/' . $directory;
}

public function getCurrectDirectoryNameFromAbsolutePath($path)
{
$explodedPath = explode('/', $path);
return end($explodedPath);
}

public function isZipFile($extension)
{
if ($extension == self::ZIP_EXTENTION) {
return true;
}

return false;
}

public function isDirectory($extension)
{
if ($extension == self::DITECTORY_EXTENTION) {
return true;
}

return false;
}
}

class PhpDirectoryIterator
{
private $fileManager;
private $nameManager;
private $modifiers = [];

public function __construct()
{
$this->fileManager = new PhpFileManager;
$this->nameManager = new PhpNameManager;
}

public function addModifier($extension, $clouusre)
{
$this->modifiers[$extension] = $clouusre;
}

public function exec(string $path)
{
$files = $this->fileManager->getFileNamesInPath($path);
foreach ($files as $fileName) {
$extension = $this->nameManager->getFileExtentionFromFileName($fileName);

if ($this->nameManager->isZipFile($extension)) {
$tempExtractedDirectoryName = $this->nameManager->getTempExtractedDirectoryName($fileName);
$this->fileManager->unzip($path, $fileName ,$tempExtractedDirectoryName);
$this->exec($this->nameManager->getAbsolutePathWithDirectory($path, $tempExtractedDirectoryName));
}

if ($this->nameManager->isDirectory($extension)) {
$this->exec($this->nameManager->getAbsolutePathWithDirectory($path, $fileName));
}

foreach ($this->modifiers as $modifierExtention => $modifierFunction) {
if ($extension == $modifierExtention) {
$modifierFunction($this->nameManager->getAbsolutePathWithDirectory($path, $fileName));
}
}
}

$folderName = $this->nameManager->getCurrectDirectoryNameFromAbsolutePath($path);
if ($this->isExtentedDirectory($folderName)) {
$zipFileName = $this->getZipFileNameFromExtendedDirectoryName($folderName);
$zipFilePath = $this->getZipFilePath($path, $folderName);

$this->fileManager->remove($zipFilePath . $zipFileName);
$this->fileManager->zip($zipFilePath, $zipFileName, $folderName);
$this->fileManager->remove($path);
}
}

public function isExtentedDirectory($DirectoryName)
{
$explodedDirectoryName = explode(PhpNameManager::ZIP_EXTENTION_WITH_DOT, $DirectoryName);
if (count($explodedDirectoryName) == 2) {
$directoryName = $explodedDirectoryName[0] . PhpNameManager::ZIP_EXTENTION_WITH_DOT;
$directoryHashedName = $explodedDirectoryName[1];
if ($directoryHashedName == $this->nameManager->hash($directoryName)) {
return true;
}
}

return false;
}

public function getZipFileNameFromExtendedDirectoryName($extentedDirectoryName)
{
$explodeDirectoryName = explode(PhpNameManager::ZIP_EXTENTION_WITH_DOT, $extentedDirectoryName);
return reset($explodeDirectoryName) . PhpNameManager::ZIP_EXTENTION_WITH_DOT;
}

public function getZipFilePath($dir, $folderName)
{
return str_replace($folderName, '', $dir);
}
}

$path = getcwd();
$object = new PhpDirectoryIterator();

$object->addModifier("txt", function ($path) {
$myfile = fopen($path, "a") or die("Unable to open file!");
$txt = "test\n";
fwrite($myfile, $txt);
fclose($myfile);
}
);

$object->exec($path);

?>

(I separate the works that are related to the file and which are related to the namings in two classes and it should work in the Unix-based distributions and you have problem with the rm , zip , unzip functions in your device you can just change these functions in the fileManager class)

If you run the php code this code will added one line to all of the text files (in the directory and in the zip files too)

result

You should see some things like it in all text files

test

If you have any questions feel free to ask and thanks for your good question

other example

For example if you want to search for the special string in the all files you can do it

$path = getcwd();
$object = new PhpDirectoryIterator();

$object->addModifier("txt", function ($path) {
if(strpos(file_get_contents($path), "Theme Name : ") !== false) {
$explodedPath = explode('/', $path);
$fileName = end($explodedPath);
echo "Found it in : " . $fileName;
}
}
);

$object->exec($path);

Finding a file within recursive directory of zip files

You can omit using find for single-level (or recursive in bash 4 with globstar) searches of .zip files using a for loop approach:

for i in *.zip; do grep -iq "mylostfile" < <( unzip -l $i ) && echo $i; done

for recursive searching in bash 4:

shopt -s globstar
for i in **/*.zip; do grep -iq "mylostfile" < <( unzip -l $i ) && echo $i; done

Search a String in folder containing zips of text files

You can use zgrep, which has the same semantics as grep, but can search within compressed files:

$ zgrep -Ril "My_Name"


Related Topics



Leave a reply



Submit