Linux zip add to archive

linux how to add a file to a specific folder within a zip file

If you need to add the file to the same folder as in the original directory hierarchy, then you just need to add the full path to it:

Otherwise, probably the easiest way to do that is to create the same layout you need in the zip file in a temporary directory.

if i wanna put yyy.txt into xxx.apk’s assests’ folder, firstly i must creat assests folder,then move yyy.txt into it. And then excute zip -g xxx.apk assests/yyy.txt

If you want to have the good hierarchy without copying files into a newly created dir, you can create a symbolic link : ln -s long_path_on_disk folder_in_zip. Then, zip -g xxx.zip folder_in_zip/file

To elaborate on @Ignacio Vazquez-Abrams answer from a year ago, you can use a lower level library, such as the one that comes with Python:

#!/bin/bash python -c ' import zipfile as zf, sys z=zf.ZipFile(sys.argv[1], "a") z.write(sys.argv[2], sys.argv[3]) z.close() ' myfile.zip source/dir/file.txt dir/in/zip/file.txt 

This will open myfile.zip and add source/dir/file.txt from the file system as dir/in/zip/file.txt in the zip file.

Info-ZIP cannot do this. You will need to write a script or program in a language that has lower-level access to zip files.

if i wanna put yyy.txt into xxx.apk’s assests’ folder, firstly i must creat assests folder,then move yyy.txt into it. And then excute zip -g xxx.apk assests/yyy.txt

Or you can generate an artificial zip entry that puts it in assets/ , then combine it with the data from the file and add it to the zip file.

I have expended a little @»that other guy» solution

Go to console, press ctrl+x,ctrl+e, paste there

( cat /tmp/zip-extend && chmod +x /tmp/zip-extend 

then run /tmp/zip-extend my_zip.zip your/existing/file_to_add.xml directory_in_zip/file_to_add.xml

cd /tmp touch first_file.txt zip my_zip.zip first_file.txt unzip -l my_zip.zip mkdir -p your/existing touch your/existing/file_to_add.xml /tmp/zip-extend my_zip.zip your/existing/file_to_add.xml directory_in_zip/file_to_add.xml unzip -l my_zip.zip cd - 
Archive: my_zip.zip Length Date Time Name --------- ---------- ----- ---- 0 2013-12-17 15:24 first_file.txt 0 2013-12-17 15:24 directory_in_zip/file_to_add.xml --------- ------- 0 2 files 

Источник

Linux zip Command Tutorial with Examples

Linux zip Command Tutorial with Examples

Zip is a compression algorithm and format used to package given files and folders to use less disk space. The zip files have generally the *.zip extension and different tools that can be used to create, add, remove or extract zip files. But the most prolific tool and command is the zip command in Linux operating systems.

zip Command Syntax

The zip command syntax is like below.

zip OPTION ZIP_FILE FILES_FOLDERS
  • OPTION is used to provide different options to the zip file.
  • ZIP_FILE is the file name of the zip file.
  • FILES_FOLDERS can be single or more files and folders which will be zipped into the ZIP_FILE.
Читайте также:  Finding file in linux directory

Install zip Command

In some cases the zip command is not installed by default. We may need to install zip command explicitly.

Install zip Command In Ubuntu, Debian, Mint, Kali:

Install zip Command In Fedora, RHEL, CentOS:

zip Command Help

The zip command is very old and actively developed command and provides detailed help information about its usage. The simplest and less informative way to get help is running the zip command without any option or parameter.

More detailed help can be listed with the -h2 option to the zip command like below.

Zip A File

Let’s start with a simple example where we zip a single file named cities.txt . The zip file is named as cities.zip like below. There is no need to add an extra parameter to zip it.

adding: cities.txt (deflated 15%)

Zip Multiple Files

We can also zip multipe files in a single step. We just provide the files we want to zip. In the following example we zip files named cities.txt , MyFile.csv and myshell.txt into zip file named my.zip .

Add New File To Zipped File

New files can be added to the existing zip file. Just provide the zip file name and the file name we want to add. In the following example we add the file numbers.txt into the zip file named my.zip .

Delete/Remove A File From Zip File

A single file can be removed from the zipped file. The -d option is used to remove the specified file. In the following example, we remove the file named myshell.txt from the zipped file named my.zip .

If the specified file is deleted or removed from the zip file the previous deleting: line is printed with the deleted file name.

Zip Files and Folders Recursively

Normally when we specify a folder to zip only the folder is zipped and its contents are not zipped. In order to zip a folder or directory with its files and child folders and directories it should be zipped recursively. The -r option is used to zip files and folders recursively.

$ zip -r Downloads.zip Downloads/

The zip command and operation executes different steps. More detailed information about the zip operation can be printed as verbose mode. The -v option is used to print detailed verbosely which can be also called as debugging the zip.

$ zip -r -v Downloads.zip Downloads/

Suppress Output Do Not Print Information

We can prevent the zip command printing information about the zip process and related files and folders names. We should suppress the output with the -q option like below.

$ zip -r -q Downloads.zip Downloads/

Источник

Adding unzipped files to a zipped folder

I’m trying to add unzipped files to an existing already zipped folder say new folder.zip . Is it possible to use zip -r new folder.zip after adding some unzipped files to them? Will this command compress the folder? Is there any alternative to do this?

you can pipe it, unzip the original file | then zip up the entire content as how you’d create a brand new file. I’m not sure how you’d do this with a single command.

Читайте также:  Ati graphics in linux

5 Answers 5

Use the update flag: -u

zip -ur existing.zip myFolder

This command will compress and add myFolder (and it’s contents) to the existing.zip .

Advanced Usage:

The update flag actually compares the incoming files against the existing ones and will either add new files, or update existing ones.

Therefore, if you want to add/update a specific subdirectory within the zip file, just update the source as desired, and then re-zip the entire source with the -u flag. Only the changed files will be zipped.

If you don’t have access to the source files, you can unzip the zip file, then update the desired files, and then re-zip with the -u flag. Again, only the changed files will be zipped.

Original Source Structure

 ParentDir ├── file1.txt ├── file2.txt ├── ChildDir │ ├── file3.txt │ ├── Logs │ │ ├── logs1.txt │ │ ├── logs2.txt │ │ ├── logs3.txt 

Updated Source Structure

 ParentDir ├── file1.txt ├── file2.txt ├── ChildDir │ ├── file3.txt │ ├── Logs │ │ ├── logs1.txt │ │ ├── logs2.txt │ │ ├── logs3.txt │ │ ├── logs4.txt <-- NEW FILE  
$ zip -ur existing.zip ParentDir > updating: ParentDir/ChildDir/Logs (stored 0%) > adding: ParentDir/ChildDir/Logs/logs4.txt (stored 96%) 

Источник

This post and this website contains affiliate links. See my disclosure about affiliate links.

how to add new files into an archive file (tar, gz, zip) in linux (also delete files)

There are several programs, both command line and GUI based that will allow you to view and extract the contents of an archive. But sometimes, you will need to modify an existing archive file, such as add files to the archive, delete an existing file in the archive or rename a file in the archive.

Most of the GUI applications, like Ark support adding and deleting files using the drag and drop. This is accomplished by extracting the archive in memory and performing the modification operation. The archive is then re-archived from memory and saved to the disk. A GUI interface may not always be available and you may want to modify the archive from command line without extracting it completely.

You can always do it the long way. First extracting the archive to the hard disk and then modify the contents and re-create the archive from the modified content. This requires space on the disk and extra time for extraction and archiving.

Most of the widely used archive formats are usually the combination of two different process: archiving and compression. There are a couple of caveats that you should be aware of while modifying the archives.

compressed archive: You can directly modify an archive but not a compressed archive. Modifying the compressed file without uncompressing it first will corrupt the file. For example, this means that you can modify a .tar file, but not a .tar.gz file. The .tar.gz should first be uncompressed to .tar.

file position: There is really no way to “replace” a file inside the archive while also maintaining its position. You can always extract the whole archive to a temp location, modify or replace the file and then archive it back up. But we are only dealing with the scenario of doing the operations in place without extracting the entire archive.

Читайте также:  How to run linux and windows

Add files to archive

tar archive

Assuming that you have a archive with .tar extension, you can use the -r (or –append) option of the tar command to add/append a new file to the end of the archive.

bash$ tar rvf /path/to/archive.tar /path/to/newfile.txt

You can use the -v option to have a verbose output to verify the operation. The other option that can be used with the tar command is -u (or –update).

The update refreshes the files that are newer than the ones in the archive, so it works slightly different than the –append (or -r) option but provides the same functionality for the addition of a new file.

bash$ tar uvf /path/to/archive.tar /path/to/newfile.txt

Another feature of the archive is that it allows for multiple files with the same name. If you append a file with the same name, then the file is just appended to the end of the archive without deleting the previous one. But when you extract the archive, it will extract the latest version (by its order inside the archive) of the file.

zip archive

The zip archive can be modified using the zip command. The -r option of the zip command allows you to add new files to the archive.

bash$ zip -rv zipfile.zip newfile.txt newfile1.txt

where the zipfile.zip is the name of an existing zip file and the newfile.txt and newfile1.txt are the files that you want to add to the zip archive.

jar archive

In case of the jar files, you can use the jar command. The –update (or -u) option allows you to add or update files into a jar archive.

bash$ jar -uvf jarfile.jar newfile.txt

Again, you can add multiple files in a single command just like the zip command.

Delete files from archive

tar archive

You can use the –delete option of the tar command to delete existing files from inside an archive.

bash$ tar -dvf archive.tar filename.txt

zip archive

You can use the –delete (or -d) option of the zip command to delete files from the archive. The zip command take multiple arguments for file names and supports regular expressions in the argument. This allows for deletion of multiple files with a single command.

bash$ zip -d zipfile.zip filename.doc \*.txt

The above command will delete the file named filename.doc and also all the files with an extension of .txt.

jar archive

Unfortunately there is no option with the jar command to delete the files. But jar files are essentially zip files with .jar extension, hence you can use the zip command to delete the files, just as you would to a zip file.

bash$ zip -d jarfile.jar file1.txt file2.txt

Renaming files inside the archive essentially amounts to deleting the existing file and adding the new file. However, this will also cause the file to lose its position inside the tar archive. If the order of the files is important, then you should extract the archive and rename the file on the disk first.

Источник

Оцените статью
Adblock
detector