import java.io.File; import java.net.URI; import java.nio.file.StandardCopyOption; import java.nio.file.FileSystem; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashMap; import java.util.Map; public class TestNIO2 { public static void main( String args[] ) throws Exception { if ( ( args == null ) || ( args.length != 1 ) ) { System.err.println( "Must specify file to add to zip" ); System.exit( 1 ); } final File file = new File( "MyTestZip.zip" ); final Map env = new HashMap<>(); if ( !file.isFile() ) env.put( "create", "true" ); final URI fileUri = file.getAbsoluteFile().toURI(); final URI uri = URI.create( "jar:" + fileUri ); try ( FileSystem zipfs = FileSystems.newFileSystem( uri, env ) ) { Path externalFile = Paths.get( args[0] ); Path pathInZipfile = zipfs.getPath( "/" + externalFile.getFileName() ); Files.copy( externalFile, pathInZipfile, StandardCopyOption.REPLACE_EXISTING ); } } }