private FileInfo scanZip(FileInfo zip) { try { File zf = new File(zip.pathname); long arcsize = zf.length(); // ZipFile file = new ZipFile(zf); ArrayList<ZipEntry> entries = engine.getArchiveItems(zip.pathname); ArrayList<FileInfo> items = new ArrayList<FileInfo>(); // for ( Enumeration<?> e = file.entries(); e.hasMoreElements(); ) { for (ZipEntry entry : entries) { if (entry.isDirectory()) continue; String name = entry.getName(); FileInfo item = new FileInfo(); item.format = DocumentFormat.byExtension(name); if (item.format == null) continue; File f = new File(name); item.filename = f.getName(); item.path = f.getPath(); item.pathname = entry.getName(); item.size = (int) entry.getSize(); // item.createTime = entry.getTime(); item.createTime = zf.lastModified(); item.arcname = zip.pathname; item.arcsize = (int) entry.getCompressedSize(); item.isArchive = true; items.add(item); } if (items.size() == 0) { L.i("Supported files not found in " + zip.pathname); return null; } else if (items.size() == 1) { // single supported file in archive FileInfo item = items.get(0); item.isArchive = true; item.isDirectory = false; return item; } else { zip.isArchive = true; zip.isDirectory = true; zip.isListed = true; for (FileInfo item : items) { item.parent = zip; zip.addFile(item); } return zip; } } catch (Exception e) { L.e("IOException while opening " + zip.pathname + " " + e.getMessage()); } return null; }
public FileInfo setSearchResults(FileInfo[] results) { FileInfo existingResults = null; for (int i = 0; i < mRoot.dirCount(); i++) { FileInfo dir = mRoot.getDir(i); if (dir.isSearchDir()) { existingResults = dir; dir.clear(); break; } } if (existingResults == null) { FileInfo dir = new FileInfo(); dir.isDirectory = true; dir.pathname = FileInfo.SEARCH_RESULT_DIR_TAG; dir.filename = mActivity.getResources().getString(R.string.dir_search_results); dir.parent = mRoot; dir.isListed = true; dir.isScanned = true; mRoot.addDir(dir); existingResults = dir; } for (FileInfo item : results) existingResults.addFile(item); return existingResults; }
/** * Adds dir and file children to directory FileInfo item. * * @param baseDir is directory to list files and dirs for * @return true if successful. */ public boolean listDirectory(FileInfo baseDir) { Set<String> knownItems = null; if (baseDir.isListed) { knownItems = new HashSet<String>(); for (int i = baseDir.itemCount() - 1; i >= 0; i--) { FileInfo item = baseDir.getItem(i); if (!item.exists()) { // remove item from list baseDir.removeChild(item); } else { knownItems.add(item.getBasePath()); } } } try { File dir = new File(baseDir.pathname); File[] items = dir.listFiles(); // process normal files if (items != null) { for (File f : items) { // check whether file is a link if (Engine.isLink(f.getAbsolutePath()) != null) { log.w("skipping " + f + " because it's a link"); continue; } if (!f.isDirectory()) { // regular file if (f.getName().startsWith(".")) continue; // treat files beginning with '.' as hidden if (f.getName().equalsIgnoreCase("LOST.DIR")) continue; // system directory String pathName = f.getAbsolutePath(); if (knownItems != null && knownItems.contains(pathName)) continue; if (engine.isRootsMountPoint(pathName)) { // skip mount root continue; } boolean isZip = pathName.toLowerCase().endsWith(".zip"); FileInfo item = mFileList.get(pathName); boolean isNew = false; if (item == null) { item = new FileInfo(f); if (isZip) { item = scanZip(item); if (item == null) continue; if (item.isDirectory) { // many supported files in ZIP item.parent = baseDir; baseDir.addDir(item); for (int i = 0; i < item.fileCount(); i++) { FileInfo file = item.getFile(i); mFileList.put(file.getPathName(), file); } } else { item.parent = baseDir; baseDir.addFile(item); mFileList.put(pathName, item); } continue; } isNew = true; } if (item.format != null) { item.parent = baseDir; baseDir.addFile(item); if (isNew) mFileList.put(pathName, item); } } } // process directories for (File f : items) { if (f.isDirectory()) { if (f.getName().startsWith(".")) continue; // treat dirs beginning with '.' as hidden FileInfo item = new FileInfo(f); if (knownItems != null && knownItems.contains(item.getPathName())) continue; item.parent = baseDir; baseDir.addDir(item); } } } baseDir.isListed = true; return !baseDir.isEmpty(); } catch (Exception e) { L.e("Exception while listing directory " + baseDir.pathname, e); baseDir.isListed = true; return false; } }