have everything working including all the password protection parts but can't get past this last issue.
The requirement is on a Laravel app, a CSV is created and downloaded as a password protected zip.
To do this I have a class that stores the CSV locally, then a method that calls the following:
echo system('zip -P ' .$this->password. ' ' .$this->getZipStoragePath(). ' ' .storage_path('app/').$this->getFilePath());
This 'works' in that it creates a password protected zip file. But when extracted, there is a folder structure of my machine up to the point to point to find this file.
So, extracting this created zip gives me a directory of:
Users > Craig > Apps > Project Name > Storage > app > temp > then the file here.
Is there a way I can use the same zip system method but have it only zip up the file and not the whole path as returned with the storage_path() method?
It seems that you want the -j flag:
-j
--junk-paths
Store just the name of a saved file (junk the path), and do
not store directory names. By default, zip will store the
full path (relative to the current directory).
Also note that you can use the native PHP Zip support, and call addFile() with the optional second argument to override the filename inside the archive.
Related
I have a zip folder on my server containing a complex file structure and hundreds of files all with different timestamps.
I've been using php's ZipArchive to extract everything I need and it's working great apart from all the timestamps once extracted are the current date/time.
How can I extract files from a zip folder using PHP while retaining
the files original timestamps?
You can try tar.gz format. If you using windows can use 7 zip
If you using Linux, there is tar default command.
Or php syntax http://www.php.net/manual/en/phardata.extractto.php
Both of them will keep the original timestamp.
I didn't find a way to do what I wanted with a zip folder.
Instead I created a .tar.gz file and used Archive_Tar class to extract the folders and files. To use Archive_Tar the server needs to be running PHP 5.3+ and have PEAR installed.
require_once 'Archive/Tar.php';
$path = '';
$import_file_path = $path."/report_import.tar.gz";
if(file_exists($import_file_path)){
$tar = new Archive_Tar($import_file_path, true);
$result = $tar->extract($path);
}
There isn't a direct way via the PHP ZipArchive library to extract files while retaining the original timestamps in the archive. However, each file in the archive's last modified timestamp is readable from the archive by reading the 'mtime' variable returned by calling ZipArchive::statName(). You could store the result of this for each file and then manually setting the timestamp of the extracted file on the filesystem's using PHP's touch() function.
You'd probably be better off calling unzip or another tool using exec(), which retains the timestamps by default or with a simple option parameter.
I am creating a zip archive in php and want to add some pdf files available in my pdfs_lib folder.I have successfully created the archive but there is a problem that inside the created zip archive, files get added into pdfs_lib folder, which is not what i want.
I want to add pdf files present in pdfs_lib folder directly into the archive instead of adding those files inside pdfs_lib folder in my created zip archive.
What i have tried is:
// Create Zip File
$zip = new ZipArchive;
$res = $zip->open("pdfs_lib/my_zip_archive.zip", ZipArchive::CREATE);
if ($res === TRUE)
{
// Add pdf file to zip archive
$zip->addFile("pdfs_lib/pdf_file_1.pdf");
$zip->addFile("pdfs_lib/pdf_file_2.pdf");
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
This code creates the zip archive successfully but whwen i unzip the archive it creates a pdfs_lib folder and unzip all files inside this newly created folder.
Any suggestion on how to create a zip archive without add pdf_lib folder in it.
Change the addFile calls to:
$zip->addFile("pdfs_lib/pdf_file_1.pdf", "pdf_file_1.pdf");
The second parameter is the 'localname', the name used in the zipfile itself. If you leave out the folderpath there, it will be added to the root in the zipfile.
A side not as an into: usually it is considered good practice if an archive (be it zip or any other archive format) unpacks into a clean folder instead of littering the contained files all over the place.
However if you really want to implement this without such folder, then you must rework your code. You have two alternatives:
you can change the working directory your php script is executed in into that folder where you keep the files in. In that case you do not need any path to add files to the zip archive, thus no folder is constructed inside the archive.
the addFile() method of phps zip extension allows to specify a second (optional) argument which allows to pass the target name of a passed file.
I want to crate a zip file and download it.
So my code is as follow;
Since i am using ci zip encoding class
my folder structure is like
c:/xampp/htdocs/gallery/print/oid_1/1.jpg,2.jpg,3.jpg
My code
$path = getcwd().'/print/oid_1/';
$this->zip->read_dir($path,FALSE);
// Download the file to your desktop. Name it "my_backup.zip"
$this->zip->download('oid_1.zip');
Here is my problem,
l C:\Users\instinctclassic\Downloads\Compressed\oid_1.zip: The archive is either in unknown format or damaged so what does this means -does it means it was mistaken while making zip file or it was mistaken while downloading.For downloading mistake i have downloaded the zip file many time enough to be sure.
2 I repaired the downloaded zip file(oid_1) and extract it but the extracted folder structure is not ignored before the oid_1 folder as said in ci zip encoding class tutorial
$this->zip->read_dir($path, FALSE);
//This will create a ZIP with the folder "directory" inside, then all sub-folders stored correctly inside that, but will not include the folders /path/to/your.
3 Assuming my folder oid_1 already exists somewhere on server.
I know this question is previously asked but mine exist all the problem from making zip file to extracting.So i am sorry for this.
Thanks
Try directory path like this:
$path = getcwd().'\print\oid_1\';
this way path will be proper, because getcwd() will return you c:\xampp\htdocs\gallery
Im moving un-zipped files to a server directory /zip/file.unzipped.png
now i need to take that file and zip it.
Is it possible to do that or a need the zip archive as codeigniter guide says?
How can i do that?
Is what you're looking for just a straight download, and not saving the archive on disk before download?
If this is the case you can do what #zer02 suggested, but remove the archive step.
$name = 'mydata1.txt';
$data = 'A Data String!';
$this->zip->add_data($name, $data);
// Write the zip file to a folder on your server. Name it "my_backup.zip"
$this->zip->archive('/path/to/directory/my_backup.zip');
// Download the file to your desktop. Name it "my_backup.zip"
$this->zip->download('my_backup.zip');
The zip library should gather up all the data you've added to the zip cache in memory, and create a file to download dynamically.
Please read the user manual: http://codeigniter.com/user_guide/libraries/zip.html
There are two functions that jump out me for your use case:
$this->zip->read_file()
Permits you to compress a file that already exists somewhere on your
server. Supply a file path and the zip class will read it and add it
to the archive:
$this->zip->read_dir()
Permits you to compress a folder (and its contents) that already
exists somewhere on your server. Supply a file path to the directory
and the zip class will recursively read it and recreate it as a Zip
archive. All files contained within the supplied path will be encoded,
as will any sub-folders contained within it.
$name = 'mydata1.txt';
$data = 'A Data String!';
$this->zip->add_data($name, $data);
// Write the zip file to a folder on your server. Name it "my_backup.zip"
$this->zip->archive('/path/to/directory/my_backup.zip');
// Download the file to your desktop. Name it "my_backup.zip"
$this->zip->download('my_backup.zip');
Zip Lib CI
I am attempting to use the ZipArchive class to unzip the contents of a zip containing a GitHub repository.
When you download a zip from GitHub, the zip contains a folder at the root of the zip named something like "project-2302392-20230"... Then within that folder are the actual contents of the repository.
Is it possible extract everything within that project folder without extracting the folder itself?
Here's one way to figure out the name of the unique top directory of the github zip archive.
use getArchiveComment() to get the ziparchive comment.
use strpos to strip away all characters except the first 7 and also add the git username and repo name like so '/USER-REPO-'.substr($comment, 0, 7);
You could also use regex, scandir, and a number of other ways.