LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-17-2009, 07:45 AM   #1
marvelade
LQ Newbie
 
Registered: Dec 2006
Location: Belgium
Distribution: Mac OS X; Ubuntu 8.04 in a VM
Posts: 29

Rep: Reputation: 16
how to add a file to a zip, without including the entire path from root


Hi All,


the following command works:
zip /var/www/html/ftp/zipdownloads/zipfile.zip /var/www/html/path/to/file1.png

But when I open the zip file on my desktop, the entire file's path-structure is saved inside the zip file so i have to go through 5 directories (var -> www -> html -> path -> to) to get to the file I zipped.

Is there a way to avoid this and have the file in the "root" of the zipfile?


thx + best regards,
Marvelade
 
Old 02-17-2009, 07:49 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Use the -j option. From man zip:
Code:
-j     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 path).
 
Old 02-17-2009, 11:09 AM   #3
marvelade
LQ Newbie
 
Registered: Dec 2006
Location: Belgium
Distribution: Mac OS X; Ubuntu 8.04 in a VM
Posts: 29

Original Poster
Rep: Reputation: 16
Thanks for the hint, colucix. This got me where I wanted on files, but now I need to expand this to also include entire directories in the zipfile. I need the path info from the working directory, but no further up the tree.
I'll try to "cd" my way into the directory that serves as base directory and then call the path from there without the absolute path.

If anything new comes up, you'll be hearing from me.

another annoyance is that, for security reasons, all files are set to permissions 040, so I need to set them to 440 before zip can even read them. With a single file, this wasn't a problem, but with an entire directory I think I'll be needing a recursive function...

Forgot to mention, I'm using PHP to execute these commands with exec(), but I guess that's not a real problem.


grtz,
Marvelade
 
Old 02-17-2009, 11:17 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
To zip an entire directory preserving its relative structure, just use the -r (recursion) option:
Code:
zip -r name_of_archive.zip path/to/dir/to/archive
To change permission recursively, use the -R option of chmod:
Code:
chmod -R 440 path/to/dir/to/archive
Regarding the PHP part of your question, unfortunately I have not experienced with this language, so I cannot be of much help. But I guess you have a kind of system function to execute commands on the native OS.
 
Old 02-18-2009, 10:53 AM   #5
marvelade
LQ Newbie
 
Registered: Dec 2006
Location: Belgium
Distribution: Mac OS X; Ubuntu 8.04 in a VM
Posts: 29

Original Poster
Rep: Reputation: 16
Hey colucix,

Thanks for the hints on zip and chmod.


Quote:
Originally Posted by colucix View Post
Regarding the PHP part of your question, unfortunately I have not experienced with this language, so I cannot be of much help. But I guess you have a kind of system function to execute commands on the native OS.
Indeed; you can call a native linux command using the php function exec() like this:


Code:
<?php

$file_to_zip = "/var/www/htdocs/pics/picture.jpg";
$zip_archive = "/var/www/htdocs/zipfiles/zip_" . mktime() . ".zip";

exec("zip -j " . $zip_archive . " " . $file_to_zip);

?>
the great advantage is that you can use PHP to manipulate your file-system through a web-page. I'm now writing a web-based FTP program in PHP, so since a lot of file handling is done, I use the exec() function regularly.

If you like to write code, PHP's a great language to learn.


Best regards,
Marvelade
 
Old 02-18-2009, 11:10 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by marvelade View Post
If you like to write code, PHP's a great language to learn.
Thanks for the advice. I will dive into PHP just after I'll have learned all the python's secrets!
 
Old 02-19-2009, 06:32 AM   #7
marvelade
LQ Newbie
 
Registered: Dec 2006
Location: Belgium
Distribution: Mac OS X; Ubuntu 8.04 in a VM
Posts: 29

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by colucix View Post
Thanks for the advice. I will dive into PHP just after I'll have learned all the python's secrets!

Maybe this can be of interest to you:
http://pecl.php.net/package/python

you can run Python code in PHP with this extension.


best regards,
Marvelade
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Thank every one. PATHCHANGER sript file Add/Remove directory to path! icecoolcorey Programming 1 06-13-2008 02:42 AM
add file to path princessotes SUSE / openSUSE 1 02-19-2008 11:32 AM
Can dd restore the entire hard disk including partition table and MBR? depam Linux - General 17 01-01-2007 08:05 PM
list entire file path in bash ajk48n Programming 1 07-26-2006 02:05 AM
Best file to edit to add to PATH compguy81 Ubuntu 3 01-05-2006 09:19 AM


All times are GMT -5. The time now is 03:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration