How to add file to zip file

How to add file to zip file

Appending files to a zip file with Java

I am currently extracting the contents of a war file and then adding some new files to the directory structure and then creating a new war file.

I can’t seem to find a way to do this in the documentation though or any online examples.

Anyone can give some tips or pointers?

TrueZip as mentioned in one of the answers seems to be a very good java library to append to a zip file (despite other answers that say it is not possible to do this).

Anyone have experience or feedback on TrueZip or can recommend other similar libaries?

13 Answers 13

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

Switch to Trending sort

In Java 7 we got Zip File System that allows adding and changing files in zip (jar, war) without manual repackaging.

We can directly write to files inside zip files as in the following example.

How to add file to zip file. Смотреть фото How to add file to zip file. Смотреть картинку How to add file to zip file. Картинка про How to add file to zip file. Фото How to add file to zip file

As others mentioned, it’s not possible to append content to an existing zip (or war). However, it’s possible to create a new zip on the fly without temporarily writing extracted content to disk. It’s hard to guess how much faster this will be, but it’s the fastest you can get (at least as far as I know) with standard Java. As mentioned by Carlos Tasada, SevenZipJBindings might squeeze out you some extra seconds, but porting this approach to SevenZipJBindings will still be faster than using temporary files with the same library.

Here’s some code that writes the contents of an existing zip (war.zip) and appends an extra file (answer.txt) to a new zip (append.zip). All it takes is Java 5 or later, no extra libraries needed.

How to add file to zip file. Смотреть фото How to add file to zip file. Смотреть картинку How to add file to zip file. Картинка про How to add file to zip file. Фото How to add file to zip file

You could use this bit of code I wrote

How to add file to zip file. Смотреть фото How to add file to zip file. Смотреть картинку How to add file to zip file. Картинка про How to add file to zip file. Фото How to add file to zip file

Michael Krauklis is correct that you cannot simply «append» data to a war file or zip file, but it is not because there is an «end of file» indication, strictly speaking, in a war file. It is because the war (zip) format includes a directory, which is normally present at the end of the file, that contains metadata for the various entries in the war file. Naively appending to a war file results in no update to the directory, and so you just have a war file with junk appended to it.

What’s necessary is an intelligent class that understands the format, and can read+update a war file or zip file, including the directory as appropriate. DotNetZip does this, without uncompressing/recompressing the unchanged entries, just as you described or desired.

How to add files/folders to multiple zip files at once with WinRAR?

Is there any way to add files/folders to multiple zip files at once. For example:

I need to add updates to all archives without opening all of them and pasting updates in there.

Any way to do this?

Or is there another archive program that can do this?

How to add file to zip file. Смотреть фото How to add file to zip file. Смотреть картинку How to add file to zip file. Картинка про How to add file to zip file. Фото How to add file to zip file

2 Answers 2

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

Switch to Trending sort

This can be done using a batch file and for example WinRAR.

For each archive*.zip file WinRAR is called to update the ZIP file with the two folders and the two files.

The batch processing finishes without printing any message and without pausing if all ZIP files found could be updated successfully. Otherwise the batch file outputs which ZIP file could not be updated for example because of read-only attribute set, and pauses the batch processing before finishing so that the user can read the error message(s).

For details on the WinRAR command u and the used switches open in WinRAR from menu Help the Help topics, open on tab Contents the item Command line mode and read at least the help pages:

For understanding the other used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

How to add a file in a specific folder (in the ZIP)

The following code, how to add a file to a zip using java:

How am I able to add the file in a specific folder in this zip-archive?

How to add file to zip file. Смотреть фото How to add file to zip file. Смотреть картинку How to add file to zip file. Картинка про How to add file to zip file. Фото How to add file to zip file

2 Answers 2

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

Switch to Trending sort

To maintain the original folder structure in zip file created by net.lingala.zip4j.ZipFile:

If not setting the zipParameters.setFileNameInZip the file is added in root of the zip file without the original directory structure.

I found the solution:

Put the file you want to add in a specific folder and then use the method «addFolder()».

All files within this folder will be added and if the same named folder is already in the zip, your folder will not be re-created, but the files will be added to the existing one

How to add file to zip file. Смотреть фото How to add file to zip file. Смотреть картинку How to add file to zip file. Картинка про How to add file to zip file. Фото How to add file to zip file

Not the answer you’re looking for? Browse other questions tagged java zip4j or ask your own question.

Linked

Related

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Learn How to Zip and Unzip Files in Linux and be a Zip Master

Read more tutorials by Michel Nzihou!

Table of Contents

Have you ever wondered how to unzip files in Linux? Or how to create and extract a zip archive? Then this tutorial is all you need to manage your zip files effectively.

Zipping is one of the best and popular techniques to compress files by reducing their size without losing quality. And in this tutorial, you’ll learn how to compress and decompress zip files in Linux, along with other tips and tricks. Ready?

Dive in and you’ll become a “zip master” at the end of this tutorial!

Table of Contents

Prerequisites

If you’re an IT pro struggling with too many password reset requests in Active Directory, check out Specops uReset, a secure SSPR solution.

The demonstrations throughout this tutorial were made in Kali Linux 2021.2. Still, you could also work with other Linux distributions, such as Debian-based (like Unbuntu or Kali Linux), CentOS, or Fedora.

Installing the Zip and Unzip Packages

To compress and decompress into ZIP files with Linux, you’ll need a couple of packages called zip and unzip. Let’s now go through how to install these packages with the apt package manager.

To install the zip and unzip packages, launch the terminal on your desktop first and run the commands below depending on the Linux distributions you’re working on.

Each of the commands below begins with the sudo command to handle elevated permissions.

For Debian

The commands below are using the apt package manager to install the zip and unzip packages.

For Fedora and CentOS

The commands below are using the yum package management tool to install the zip and unzip packages.

Compressing Files with the Zip Package

Now that you’ve installed all the necessary packages, it’s time to learn how to compress files. To do so, you’ll run the zip command, which allows you to compress files into ZIP packages, both via the command line and via the GUI. Let’s go over each method!

Zipping Files via Command Line

In Linux, you’re typically going to be using the command line quite a bit so let’s first see how to zip files via the command line.

Notice the command syntax below. You’ll see that after the zip command, outpufile represents the name of the zip file. Then followed by the name of the files and folder ( file1 file2 folder ) to include in the zip file.

To demonstrate zipping files on the command line, open a terminal first and create a directory called

/mydir. Then create two files and a folder named test1, test2, and myfolder inside

1. Run the mkdir command below to create the mydir directory.

2. The commands below create ( touch ) the test1.txt and test2.txt files. Then create ( mkdir ) the myfolder directory.

3. Run the command below to list ( ls ) the contents of the working directory (

/mydir), as shown below.

Now that you’ve created the files and folders, compress the

/mydir folder by running the command below.

Perhaps you only plan to compress the

/mydir/myfolder directories. To do that, run the command below.

Deleting the Original File after Archiving

After running the command, you’ll notice that the zip command removes the original files.

Adding New Files to an Existing Zip File

Perhaps you forgot to include a file while compressing files to a ZIP file. How would you add a new file to an existing zip file? You could delete the zip file and create another one, but there’s a better way.

/mydir directory, then adding them to the ZIP package.

Deleting Files from a Zip File

Zipping Files via GUI

By now, you’ve learned how to zip files using the command line. But if you’re more comfortable working with a graphical interface, you’re in luck. The GNU Network Object Model Environment (GNOME) environment lets you create a zip archive with a few clicks. GNOME is a desktop GUI on top of your operating system. So, let’s get started!

2. Look for the files to zip by navigating inside the

3. To create the ZIP package, select all the files to zip, then right-click on any of the files and choose Compress from the context menu.

Since you’re creating a zip file, select the .zip option. Then create the zip file by giving it a name (newfiles), and click on Create.

The zip file is automatically created inside the

/mydir folder, where the files you’re compressing are located.

You’ll notice below that Linux creates a file called newfiles.zip from those that you selected.

Decompressing Files with the Unzip Package

In the previous examples, you’ve learned how to zip files. But perhaps you need to extract or decompress the contents of the zip file. How would you do that?

Similar to compressing files, you get two methods for decompressing files, via Command-Line and GUI. Both methods leverage the unzip package you previously installed to decompress ZIP files.

Unzipping Files via Command Line

Since you’ll still be running commands in a command-line environment throughout this tutorial, let’s start unzipping files via the command-line method.

To unzip a ZIP file, launch your terminal and run the following command. Notice the command below will unzip the contents of the ( newdir.zip ) ZIP file. By default, the contents are extracted to the same directory (

/mydir) of the ( newdir.zip ) ZIP file.

Unzipping Files via GUI

If unzipping files via command-line seems like a lot of work, try unzipping files in a few clicks with the GUI method. And similar to the command-line method, you’d also get to choose where to extract the files.

To unzip files, open File Manager, as explained in the Zipping Files via GUI section. Right click the ZIP package you’d like to extract, and select Extract Here, as shown below.

Once you click Extract Here, Linux will extract all files in the ZIP package in the working directory.

If you prefer to extract all files on another directory, choose the Extract to option from the context menu.

Verifying the Zip File Integrity

In some instances, ZIP files can become corrupt, such as downloading the ZIP file from the internet. Or when the ZIP file was transferred to a corrupted storage device. You may not know they’re corrupted on the surface, so it’s important to verify their integrity.

The command below only verifies the ZIP file’s integrity, but not the files and subdirectories inside it.

As shown below, you’ll see an indication that the test was successful.

Testing the Compressed Files Before Extracting

Extracting a ZIP file with corrupted files would only waste time and result in errors, especially when dealing with a large zip file with multiple files. So, it’s a good idea to test the compressed files in the ZIP file before extracting them.

Testing the compressed files is similar to verifying the ZIP file’s integrity, but this time you’re testing each file inside the ZIP file and not just the ZIP file itself.

As you see in the image below, the unzip package found no errors.

Viewing Zip File Contents Without Extracting

Notice the screenshot below that displays a table list of the contents inside the ( newdir.zip ) ZIP file.

Zipping Files with the Same File Extension

To demonstrate how to script some common use cases with the zip utility, perhaps you have a directory with many different files but only need to compress those that match a specific extension. To do that, you need a script that will repeatedly search for all files with the same extension. When found, adds them to a zip file.

Let’s go through what the script below does and how to run it.

The script below declares a variable ( files ) to hold the file extension value ( *.txt ), then another variable ( targetDir ) for the target directory ( anotherDir ).

1. Open a text editor and save the script with the .sh extension (ex: script.sh), which is the extension for the files written with the shell programming language.

2. Ensure that you can run the script by modifying its permission. To do that, run the chmod command to modify ( +x ) the script’s ( script.sh ) execute permission.

3. Now execute the script by running the command below. The below command runs the script (script.sh) as long as the script is inside the current working directory.

As you notice, there is a warning message telling you that the

/mynewfiles.zip does not exist. But don’t worry, as the script will automatically create the zip file.

Conclusion

If you’re an IT pro struggling with too many password reset requests in Active Directory, check out Specops uReset, a secure SSPR solution.

Throughout this tutorial, you’ve learned how to manage zip files (compress and decompress) and a practice example of how to automate the process of zipping files. As promised, you are now a “zip master”!

Now would you either turn to manual or automated processes the next time you’re zipping?

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

More from ATA Learning & Partners

Recommended Resources!

Recommended Resources for Training, Information Security, Automation, and more!

Get Paid to Write!

ATA Learning is always seeking instructors of all experience levels. Regardless if you’re a junior admin or system architect, you have something to share. Why not write on a platform with an existing audience and share your knowledge with the world?

ATA Learning Guidebooks

ATA Learning is known for its high-quality written tutorials in the form of blog posts. Support ATA Learning with ATA Guidebook PDF eBooks available offline and with no ads!

How do I add a file to a zipped file when using 7-zip?

I used to be able to drag & drop and add a file to a zipped folder. Now I cant when using 7-zip. I upgraded to a new computer with windows 8 (nightmare) that has 7-zip and I would either like to be able to add files to a zipped folder or install the old version of zipping files from Windows 7.

The «add» button inside the zipped folder on 7-zip does not work. It seems once I select the files I want to zip I am not allowed to add additional files to the zipped folder. I used to be able to do this using windows 7.

I don’t understand a lot of computer lingo like «archive» «extract» «directory» (I really try to but seriously, I just get more confused when I try to look this stuff up) so please simplify any answers as best as you can. I apologize if my answer is already listed somewhere, I did look but only got more frustrated and confused.

2 Answers 2

All you need to do is drag the file you want to add into the 7-zip file manager window (the one you get if you right-click the zip folder and choose 7-Zip->Open Archive)

How to add file to zip file. Смотреть фото How to add file to zip file. Смотреть картинку How to add file to zip file. Картинка про How to add file to zip file. Фото How to add file to zip file

Well, a «directory» is just another name for a folder, and an «archive» is another name for a zipped folder. To «extract» a zipped folder just means to get the files out of it, by whatever method you choose. Now, to business: The 7-Zip documentation includes some information on how to do what you’re attempting.

Allows you to specify options for creating or updating an archive.

How to call this dialog box

In Windows Explorer or in 7-Zip, right-click the file(s) or folder(s) you want to compress. Point to 7-Zip, and then click the Add to archive. command item.

Источники информации:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *