import java.io.File;
import java.io.OutputStream;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.HashMap;
import java.util.Map;

public class ZipFileSystemBug {
	public static void main(String[] args) throws Exception { 

		// Preparation: Create a Zip file with an entry larger than 2^31 bytes. 
		File file = File.createTempFile("large", ".zip"); 
		file.delete(); 
		final URI uri = URI.create("jar:" + file.toURI()); 
		final Map<String, Object> env = new HashMap<>(); 
		env.put("create", "true"); 
		// Uncomment this for a temporary fix using the file system instead of memory (undocumented feature?) 
		// env.put("useTempFile", Boolean.TRUE); 
		System.out.println(uri); 
		try (FileSystem fs = FileSystems.newFileSystem(uri, env)) { 
			byte[] buf = new byte[4096]; 
			try (OutputStream out = Files.newOutputStream(fs.getPath("large"), StandardOpenOption.CREATE)) { 
				for (int i = 0; i < 524289; i++) { 
					out.write(buf); 
				} 
			} 
		} 

		// Try to rewrite that file: 
		try (FileSystem fs = FileSystems.newFileSystem(uri, env)) { 
			Files.newOutputStream(fs.getPath("large"), StandardOpenOption.WRITE); 
		} finally { 
			// Cleanup. 
			file.delete(); 
		} 
	} 
}
