Codeigniter Zip Library how to zip existent file - php

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

Related

PHP - Created a zip file from a single file

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.

php ZipArchive - adding only files not the parent folder

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.

ci zip encoding class not working properly

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

Generating files/folders?

This might be a horrifically stupid question, but how would I go about doing this in PHP:
I'd like to make a .zip file dynamically, then serve it as a download immediately with force-download mime or something, with the contents of $string, with a folder structure.
Premade folder structure license\keys\{$_SERVER['REMOTE_ADDR']}\{$fn}.cf and {$fn}.cf would contain the contents of $string, inside the .zip file
I have literally no idea where to start.
You could use the ZipArchive class. Use it to create a temporary ZIP file, close the ZipArchive, stream the temporary ZIP file to the user, and delete the temporary ZIP.

I want php zip class extension to compress a directory , but without temp files

I want an extension to compress directory with all files and sub-dirs inside it, but without creating a temp files, i want the extrnsion name with a code for how to use it ?
i have an errors when the class making a temp files
Thank you ,
You want http://pablotron.org/software/zipstream-php/. From the documentation:
1. Create the zip stream:
$zip = new ZipStream('example.zip');
2. Add one or more files to the archive:
# add first file
$data = file_get_contents('some_file.gif');
$zip->add_file('some_file.gif', $data);
# add second file
$data = file_get_contents('some_file.gif');
$zip->add_file('another_file.png', $data);
3. Finish the zip stream:
$zip->finish();
I will warn you: if you're dealing with very large files the zip will break. it works well on small files, but not on 10s of megabytes.

Categories