RMRMaster RMRMaster - 1 year ago 70
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

home/myName/folderTest
(in Ubuntu). Inside this
folderTest
, 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();
System.out.println(dirPath);

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


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();

zipFile.addFolder(dirPath);

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