How to make the user enter the name of the file that will be used? Java

0

Good afternoon this program compresses folders in zip, I would like to know how I can do so that the program asks me the name of the folder that is going to be compressed and compresses only the one that the user enters his name and the zip file he has returned the same name of the folder before compressing it, all the folders will be in a root directory, I appreciate your help, attach the code:

String pathZipInput = "C:/ZIP/pro";
String pathOutputZip = "/ZIP/pro/pro.zip";{
try {
      String path="C:/ZIP/pro";                                
      File folder = new File(path);                           
     String folderName=folder.getAbsoluteFile().getName();     
      System.out.println(folderName);                         

FileSystemView fsv = new DirectoryRestrictedFileSystemView(new File("C:/ZIP/pro"));

ZipFile zipFile = new ZipFile(pathOutputZip);               
   ZipParameters parameters = new ZipParameters();
       parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
       parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);

 zipFile.addFolder(pathZipInput, parameters);
} 
catch (ZipException e) {
   JOptionPane.showMessageDialog(null, "Error: " + e.getMessage());
   e.printStackTrace();
   }
}
}
}
    
asked by HugoCo 05.06.2017 в 21:49
source

1 answer

0

You can use a scanner.

String pathZipInput = "C:/ZIP/pro";
String pathOutputZip = "/ZIP/pro/pro.zip";{
try {
    String path="C:/ZIP/pro";                                
    File folder = new File(path);                           
    String folderName=folder.getAbsoluteFile().getName();     
    System.out.println(folderName);
    Scanner scaner = new Scanner(System.in);
    System.out.print("Inserte nombre del fichero: ");
    String filename = scaner.next();
    FileSystemView fsv = new DirectoryRestrictedFileSystemView(new File("C:/ZIP/pro"));

   ZipFile zipFile = new ZipFile(path+"/"+filename);               
   ZipParameters parameters = new ZipParameters();
   parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
   parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);

   zipFile.addFolder(pathZipInput, parameters);
} 
catch (ZipException e) {
   JOptionPane.showMessageDialog(null, "Error: " + e.getMessage());
   e.printStackTrace();
   }
}
}
}
    
answered by 05.06.2017 в 22:10