100100import java .net .URLDecoder ;
101101import java .nio .file .Files ;
102102import java .nio .file .Path ;
103+ import java .nio .file .StandardCopyOption ;
103104import java .text .DateFormat ;
104105import java .text .SimpleDateFormat ;
105106import java .util .ArrayList ;
@@ -230,11 +231,11 @@ protected byte[] unzip(ZipInputStream stream)
230231 }
231232 }
232233
233- protected static boolean extractDocsFromZip (File zipFile , File containerDir ) throws IOException
234+ protected static boolean extractDocsFromZip (Path zipFile , Path containerDir ) throws IOException
234235 {
235- File docsDir = new File ( containerDir , "docs" );
236+ Path docsDir = containerDir . resolve ( "docs" );
236237 boolean extracted = false ;
237- try (ZipFile zf = new ZipFile (zipFile ))
238+ try (ZipFile zf = new ZipFile (zipFile . toFile () ))
238239 {
239240 Enumeration <? extends ZipEntry > entries = zf .entries ();
240241 while (entries .hasMoreElements ())
@@ -247,15 +248,14 @@ protected static boolean extractDocsFromZip(File zipFile, File containerDir) thr
247248 String relativePath = name .substring ("tool-inf/docs/" .length ());
248249 if (relativePath .isEmpty ())
249250 continue ;
250- File destFile = new File ( docsDir , relativePath );
251+ Path destPath = docsDir . resolve ( relativePath ). normalize ( );
251252 // Zip-slip protection
252- if (!destFile . getCanonicalPath (). startsWith (docsDir .getCanonicalPath () + File . separator ))
253+ if (!destPath . startsWith (docsDir .normalize () ))
253254 throw new IOException ("Zip entry outside target directory: " + name );
254- Files .createDirectories (destFile .getParentFile ().toPath ());
255- try (InputStream in = zf .getInputStream (entry );
256- FileOutputStream out = new FileOutputStream (destFile ))
255+ Files .createDirectories (destPath .getParent ());
256+ try (InputStream in = zf .getInputStream (entry ))
257257 {
258- in . transferTo ( out );
258+ Files . copy ( in , destPath , StandardCopyOption . REPLACE_EXISTING );
259259 }
260260 extracted = true ;
261261 }
@@ -265,12 +265,12 @@ protected static boolean extractDocsFromZip(File zipFile, File containerDir) thr
265265
266266 public static File makeFile (Container c , String filename )
267267 {
268- return new File ( getLocalPath (c ), FileUtil .makeLegalName (filename ));
268+ return getLocalPath (c ). resolve ( FileUtil .makeLegalName (filename )). toFile ( );
269269 }
270270
271- public static File getLocalPath (Container c )
271+ public static Path getLocalPath (Container c )
272272 {
273- return FileContentService .get ().getFileRootPath (c , FileContentService .ContentType .files ). toFile () ;
273+ return FileContentService .get ().getFileRootPath (c , FileContentService .ContentType .files );
274274 }
275275
276276 protected Container makeContainer (Container parent , String folderName , List <User > users , Role role ) throws IOException
@@ -417,7 +417,7 @@ public static ArrayList<String> getToolRelevantUsers(SkylineTool tool, Role[] ro
417417 return new ArrayList <>(users );
418418 }
419419
420- public static HashMap <String , String > getSupplementaryFiles (SkylineTool tool )
420+ public static HashMap <String , String > getSupplementaryFiles (SkylineTool tool ) throws IOException
421421 {
422422 // Store supporting files in map <url, icon url>
423423 final String [] knownExtensions = {"pdf" , "zip" };
@@ -435,15 +435,15 @@ public static HashMap<String, String> getSupplementaryFiles(SkylineTool tool)
435435 return suppFiles ;
436436 }
437437
438- public static HashSet <String > getSupplementaryFileBasenames (SkylineTool tool )
438+ public static HashSet <String > getSupplementaryFileBasenames (SkylineTool tool ) throws IOException
439439 {
440440 HashSet <String > suppFiles = new HashSet <>();
441- File localToolDir = getLocalPath (tool .lookupContainer ());
442- for ( String suppFile : localToolDir .list ())
441+ Path localToolDir = getLocalPath (tool .lookupContainer ());
442+ try ( var stream = Files .list (localToolDir ))
443443 {
444- final String basename = new File ( suppFile ). getName ();
445- if (! basename .startsWith ("." ) && !basename .equals (tool .getZipName ()) && !basename .equals ("icon.png" ) && !basename .equals ("docs" ))
446- suppFiles . add ( suppFile );
444+ stream . map ( p -> p . getFileName (). toString ())
445+ . filter ( name -> ! name .startsWith ("." ) && !name .equals (tool .getZipName ()) && !name .equals ("icon.png" ) && !name .equals ("docs" ))
446+ . forEach ( suppFiles :: add );
447447 }
448448 return suppFiles ;
449449 }
@@ -622,12 +622,12 @@ else if (!getContainer().hasPermission(getUser(), InsertPermission.class))
622622 tool .writeIconToFile (makeFile (c , "icon.png" ), "png" );
623623
624624 // Extract docs from tool-inf/docs/ in the ZIP; carry forward from previous version if absent
625- boolean hasDocs = extractDocsFromZip (storedZip , getLocalPath (c ));
625+ boolean hasDocs = extractDocsFromZip (storedZip . toPath () , getLocalPath (c ));
626626 if (!hasDocs && existingVersionContainer != null )
627627 {
628- File oldDocs = new File ( getLocalPath (existingVersionContainer ), "docs" );
629- if (oldDocs .isDirectory ())
630- FileUtils .copyDirectory (oldDocs , new File ( getLocalPath (c ), "docs" ));
628+ Path oldDocs = getLocalPath (existingVersionContainer ). resolve ( "docs" );
629+ if (Files .isDirectory (oldDocs ))
630+ FileUtil .copyDirectory (oldDocs , getLocalPath (c ). resolve ( "docs" ));
631631 }
632632
633633 if (copyFiles != null && existingVersionContainer != null )
0 commit comments