简体   繁体   中英

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.

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"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM