FileFactory { @NotNull @ThreadSafe public FileResource createFile( @NotNull IndexingConfig config, @NotNull Path path) throws ParseException, FileNotFoundException { Util.checkNotNull(config, path); try { PathParts pathParts = path.splitAtExistingFile(); File leftFile = pathParts.getLeft().getCanonicalFile(); if (pathParts.getRight().isEmpty()) return new PermanentFileResource(leftFile); final FileResource[] cachedResource = new FileResource[1]; PathParts cacheSplitParts = path.splitFromRight(new Predicate<Path>() { public boolean apply(Path path) { cachedResource[0] = unpackCache.get(path); return cachedResource[0] != null; } }); if (cacheSplitParts == null) { FileResource archiveResource = new PermanentFileResource(leftFile); Path archivePath = pathParts.getLeft(); String entryPath = pathParts.getRight(); return unpackFromArchive( config, archivePath, archiveResource, entryPath); } if (cacheSplitParts.getRight().isEmpty()) return cachedResource[0]; Path archivePath = cacheSplitParts.getLeft(); String entryPath = cacheSplitParts.getRight(); return unpackFromArchive( config, archivePath, cachedResource[0], entryPath); } catch (FileNotFoundException e) { String msg = Msg.file_not_found.get() + " " + path.getPath(); throw new FileNotFoundException(msg); } catch (ArchiveEncryptedException e) { String msg = Msg.archive_encrypted.get(); throw new ParseException(msg, e); } catch (DiskSpaceException e) { throw new ParseException(e); } catch (IOException e) { throw new ParseException(e); } } FileFactory(@NotNull HotColdFileCache unpackCache); @NotNull @ThreadSafe FileResource createFile(	@NotNull IndexingConfig config,
									@NotNull Path path);  }
@Test public void testUnpack() throws Exception { String testFile = TestFiles.archive_entry_7z_zip_rar.getPath(); String[] paths = { testFile, "./" + testFile, "../" + Util.USER_DIR.getName() + "/" + testFile, Util.getAbsPath(testFile), TestFiles.archive_entry_zip_zip.getPath() }; IndexingConfig config = new IndexingConfig(); for (String path : paths) { HotColdFileCache unpackCache = new HotColdFileCache(20); FileFactory fileFactory = new FileFactory(unpackCache); File file = fileFactory.createFile(config, new Path(path)).getFile(); assertTrue(file.exists()); file.delete(); } }
@Test public void testUnpackCache() throws Exception { String testFile = TestFiles.archive_entry_7z_zip_rar.getPath(); Set<File> files = new HashSet<File>(); HotColdFileCache unpackCache = new HotColdFileCache(20); FileFactory fileFactory = new FileFactory(unpackCache); IndexingConfig config = new IndexingConfig(); for (int i = 0; i < 5; i++) { files.add(fileFactory.createFile(config, new Path(testFile)).getFile()); String absPath = Util.getAbsPath(testFile); files.add(fileFactory.createFile(config, new Path(absPath)).getFile()); } assertEquals(1, files.size()); }
@Test public void testUnpackCacheLimit() throws Exception { final int cacheSize = 20; HotColdFileCache unpackCache = new HotColdFileCache(cacheSize); FileFactory fileFactory = new FileFactory(unpackCache); IndexingConfig config = new IndexingConfig(); File archive = TestFiles.simple_7z.get(); for (int i = 0; i < cacheSize * 2; i++) { File archiveCopy = Util.createDerivedTempFile("test.7z", Util.TEMP_DIR); Files.copy(archive, archiveCopy); String path = Util.joinPath(Util.getAbsPath(archiveCopy), "test.txt"); FileResource fileResource = fileFactory.createFile(config, new Path(path)); File tempFile = fileResource.getFile(); assertTrue(tempFile.isFile()); fileResource.dispose(); int actualCacheSize = unpackCache.getActualCacheSize(); assertTrue(0 < actualCacheSize); assertTrue(actualCacheSize <= cacheSize); } }
@Test public void testUnpackSfxArchives() throws Exception { HotColdFileCache unpackCache = new HotColdFileCache(20); FileFactory fileFactory = new FileFactory(unpackCache); IndexingConfig config = new IndexingConfig(); config.setDetectExecutableArchives(true); String[] paths = { TestFiles.sfx_zip.getPath() + "/test.txt", TestFiles.sfx_7z.getPath() + "/test.txt" }; for (String path : paths) assertTrue(fileFactory.createFile(config, new Path(path)).getFile().isFile()); }
@Test(expected=ParseException.class) public void testUnpackSfxRarArchive() throws Exception { HotColdFileCache unpackCache = new HotColdFileCache(20); FileFactory fileFactory = new FileFactory(unpackCache); IndexingConfig config = new IndexingConfig(); fileFactory.createFile(config, new Path(TestFiles.sfx_rar.getPath() + "/test.txt")); }
@Test public void testUmlauts() throws Exception { HotColdFileCache unpackCache = new HotColdFileCache(20); FileFactory fileFactory = new FileFactory(unpackCache); IndexingConfig config = new IndexingConfig(); String path = TestFiles.umlauts.getPath(); FileResource fileResource = fileFactory.createFile(config, new Path(path)); assertTrue(fileResource.getFile().isFile()); fileResource.dispose(); }