0

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.

RMRMaster
  • 89
  • 1
  • 11
  • What have you tried already? https://stackoverflow.com/questions/617414/how-to-create-a-temporary-directory-folder-in-java and https://stackoverflow.com/questions/106770/standard-concise-way-to-copy-a-file-in-java – Caramiriel Jul 26 '17 at 12:37
  • I am not sure what effort you put in searching – ayush Jul 26 '17 at 12:38
  • How to create a temp folder - https://stackoverflow.com/questions/617414/how-to-create-a-temporary-directory-folder-in-java – ayush Jul 26 '17 at 12:38
  • I add the code that i build for know (i edit the original question) – RMRMaster Jul 26 '17 at 12:38
  • How to add a folder/file inside zip - https://stackoverflow.com/questions/22461428/how-to-add-a-file-in-a-specific-folder-in-the-zip – ayush Jul 26 '17 at 12:39
  • If you mean copy a zip file into the folder, then copying the zip file is like copying any other file. If you mean copying a folder into a zip file - well, there are no folders in zip. Only files with names that *look* like they are in folders. – RealSkeptic Jul 26 '17 at 13:00
  • I mean: copy a folder (in zip format) to temporary folder – RMRMaster Jul 26 '17 at 13:12

1 Answers1

0

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"))
ayush
  • 14,350
  • 11
  • 53
  • 100
  • Thanks for the reply. I add a new edit (EDIT 2). The temporary folder is successful create, i dont have any exception in zip try/catch (if i change the string with the zip path for example we give me a error) but the zip file is not copy to the temporary folder – RMRMaster Jul 26 '17 at 13:23
  • sorry but i am not sure what do you exactly mean by this - "but the zip file is not copy to the temporary folder"?. Can you please elaborate.. – ayush Jul 26 '17 at 13:29
  • Yes. So i have a folder in my computer with some files and i compress this folder to zip format. Now with this code i need to create a temporary folder (this part is working now) and i need to copy the zip folder that exists in my computer to the temporary folder. Do you understand? :) – RMRMaster Jul 26 '17 at 13:33
  • Thanks for the help ;) – RMRMaster Jul 26 '17 at 15:50