RMRMaster RMRMaster - 1 year ago 87
Java Question

How to create temporary folder with zip folder inside

What I need is to create a temporary folder and put inside a zip folder that already exits in my computer.

I did not found any solution or example to build this code.

I created a folder in

(in Ubuntu). Inside this
, I create a temporary folder. Now I need to get my zip and put it inside this folder. I don't know how to do this and how to delete the temporary folder when I don't need it.

public static void main(String[] args) throws IOException {
Path rootDirectory = FileSystems.getDefault().getPath("/home/myName/folderTest");
Path tempDirectory = Files.createTempDirectory(rootDirectory, "");
String dirPath = tempDirectory.toString();

ZipFile zipFile = new ZipFile("/home/myName/zipTest.zip");
ZipParameters parameters = new ZipParameters();
zipFile.addFolder(dirPath, parameters);
catch (ZipException e) {

This code doesn't give any errors, but the zip is not copied to the temporary folder.

Answer Source

Using zip4j you can do something like this -

ZipFile zipFile = new ZipFile(source);
ZipParameters parameters = new ZipParameters();


Here source is the Path of your zip file that you said already exists.

How to copy the zip file to this destination. you can do in many ways..Simplest is --

org.apache.commons.io.FileUtils.copyFile(File, File)

FileUtils.copyFile(new File("/sourcefolder/some.zip"), 
   new File("/destination/some.zip"))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download