ArchiveFileUtils { public static Path writeFsFileToTemp(final FsDirectoryEntry fsFile, final Path tempDir) throws IOException { final Path tempFilePath = tempDir.resolve(fsFile.getName()); if (fsFile.isFile() && fsFile.getFile().getLength() > 0) { int sourceFileOffset = 0; final long fileLength = fsFile.getFile().getLength(); final int fileLengthMod = (int) fileLength % FS_READ_BYTE_BUFFER_SIZE; int blockSize = fileLengthMod == 0 ? FS_READ_BYTE_BUFFER_SIZE : fileLengthMod; final ByteBuffer bb = ByteBuffer.allocateDirect(blockSize); fsFile.getFile().read(sourceFileOffset, bb); bb.flip(); try (final FileChannel wChannel = FileChannel.open(tempFilePath, EnumSet.of(StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING))) { wChannel.write(bb); sourceFileOffset += blockSize; if (sourceFileOffset < fileLength) { ByteBuffer maxBuffer = ByteBuffer.allocateDirect((int) FS_READ_BYTE_BUFFER_SIZE); while (sourceFileOffset < fileLength) { fsFile.getFile().read(sourceFileOffset, maxBuffer); maxBuffer.flip(); wChannel.write(maxBuffer); sourceFileOffset += FS_READ_BYTE_BUFFER_SIZE; maxBuffer.clear(); } } } } return tempFilePath; } private  ArchiveFileUtils(); static URI toZipUri(URI parent, String zipEntry); static URI toIsoImageUri(URI parent, String imageEntry); static URI toRarUri(URI parent, String rarEntry); static URI toTarUri(URI parent, String tarEntry); static URI toSevenZUri(URI parent, String sevenZipEntry); static URI toFatImageUri(URI parent, String fatEntry); static URI toWebArchiveUri(String archiveType, URI parent, String warcEntry); static Path writeEntryToTemp(final Path tempDir, final ByteBuffer buffer,
                                        final ReadableByteChannel channel); static Path writeFsFileToTemp(final FsDirectoryEntry fsFile, final Path tempDir); static Path writeEntryToTemp(final Path tempDir, final byte[] buffer,
                                        final InputStream in); static URI toGZipUri(URI parent); static URI toBZipUri(URI parent); static URI toReplayUri(URI requestUri); static List<String> getAncestorPaths(String path); static int copyToBuffer(final WindowReader reader, final long position,
                                   final byte[] buffer, final int offset, final int length); static int copyToBuffer(final WindowReader reader, final long position,
                                   final ByteBuffer buffer);  }
@Test public void writeFsFileToTemp() throws IOException { Path p = Paths.get("./src/test/resources/fat12.img"); FileDisk fileDisk = new FileDisk(p.toFile(), true); FileSystem fatSystem = FileSystemFactory.create(fileDisk, true); Map<FsDirectoryEntry, Path> tempFilePathMap = new HashMap<>(); FsDirectory root = fatSystem.getRoot(); for (FsDirectoryEntry de : root) { if (de.isFile()) { tempFilePathMap.put(de, ArchiveFileUtils.writeFsFileToTemp(de, new File(System.getProperty("java.io.tmpdir")).toPath())); } } for (Map.Entry<FsDirectoryEntry, Path> savedFile : tempFilePathMap.entrySet()) { assertThat("Saved file same as length:" + savedFile.getKey().getName(), savedFile.getKey().getFile().getLength(), equalTo(savedFile.getValue().toFile().length())); assertThat(savedFile.getKey().getName()+": deleted", savedFile.getValue().toFile().delete(), equalTo(true)); } assertThat("All files from FsDirectory written to temp file", tempFilePathMap.size(), equalTo(5)); }