Search in sources :

Example 1 with IOFileFilter

use of org.apache.commons.io.filefilter.IOFileFilter in project atlas by alibaba.

the class TPatchTool method copyMainBundleResources.

/**
     * 复制主bundle的增量资源
     *
     * @param newApkUnzipFolder
     * @param baseApkUnzipFolder
     * @param patchTmpDir
     * @throws IOException
     */
private void copyMainBundleResources(final File newApkUnzipFolder, final File baseApkUnzipFolder, File patchTmpDir) throws IOException {
    boolean resoureModified = false;
    Collection<File> retainFiles = FileUtils.listFiles(newApkUnzipFolder, new IOFileFilter() {

        @Override
        public boolean accept(File file) {
            String relativePath = PathUtils.toRelative(newApkUnzipFolder, file.getAbsolutePath());
            if (pathMatcher.match(DEFAULT_NOT_INCLUDE_RESOURCES, relativePath)) {
                return false;
            }
            if (null != notIncludeFiles && pathMatcher.match(notIncludeFiles, relativePath)) {
                return false;
            }
            return true;
        }

        @Override
        public boolean accept(File file, String s) {
            return accept(new File(file, s));
        }
    }, TrueFileFilter.INSTANCE);
    for (File retainFile : retainFiles) {
        String relativePath = PathUtils.toRelative(newApkUnzipFolder, retainFile.getAbsolutePath());
        File baseFile = new File(baseApkUnzipFolder, relativePath);
        if (isFileModify(retainFile, baseFile)) {
            resoureModified = true;
            File destFile = new File(patchTmpDir, relativePath);
            FileUtils.copyFile(retainFile, destFile);
        }
    }
    if (resoureModified) {
        File AndroidMenifestFile = new File(newApkUnzipFolder, ANDROID_MANIFEST);
        FileUtils.copyFileToDirectory(AndroidMenifestFile, patchTmpDir);
    }
}
Also used : IOFileFilter(org.apache.commons.io.filefilter.IOFileFilter) File(java.io.File)

Example 2 with IOFileFilter

use of org.apache.commons.io.filefilter.IOFileFilter in project atlas by alibaba.

the class NativeSoUtils method getAbiSoFiles.

/**
     * 校验指定abi下的so文件的目录
     *
     * @param supportAbis
     * @param removeSoFiles
     * @param dirs
     * @return
     */
public static Map<String, Multimap<String, File>> getAbiSoFiles(Set<String> supportAbis, Set<String> removeSoFiles, List<File> dirs) {
    Map<String, Multimap<String, File>> result = new HashMap<String, Multimap<String, File>>();
    IOFileFilter filter = new NativeSoFilter(supportAbis, removeSoFiles);
    for (File dir : dirs) {
        Collection<File> files = FileUtils.listFiles(dir, filter, TrueFileFilter.TRUE);
        for (File file : files) {
            File parentFolder = file.getParentFile();
            String parentName = parentFolder.getName();
            String shortName = getSoShortName(file);
            Multimap<String, File> maps = result.get(parentName);
            if (null == maps) {
                maps = HashMultimap.create(10, 3);
            }
            maps.put(shortName, file);
            result.put(parentName, maps);
        }
    }
    return result;
}
Also used : HashMultimap(com.google.common.collect.HashMultimap) Multimap(com.google.common.collect.Multimap) IOFileFilter(org.apache.commons.io.filefilter.IOFileFilter) File(java.io.File)

Example 3 with IOFileFilter

use of org.apache.commons.io.filefilter.IOFileFilter in project atlas by alibaba.

the class NativeSoUtils method copyLocalNativeLibraries.

/**
     * @param localNativeLibrariesDirectory
     * @param destinationDirectory
     * @param supportAbis 支持的架构种类
     * @param removeSoFiles
     * @param mLogger
     */
public static void copyLocalNativeLibraries(final File localNativeLibrariesDirectory, final File destinationDirectory, Set<String> supportAbis, Set<String> removeSoFiles, ILogger mLogger) {
    mLogger.info("Copying existing native libraries from " + localNativeLibrariesDirectory + " to " + destinationDirectory);
    try {
        IOFileFilter filter = new NativeSoFilter(supportAbis, removeSoFiles);
        // 首先判断是否存在着同名的文件存在,如果存在是否大小不一致
        Collection<File> files = FileUtils.listFiles(localNativeLibrariesDirectory, filter, TrueFileFilter.TRUE);
        List<String> dumpFiles = new ArrayList<String>();
        for (File file : files) {
            String relativePath = getRelativePath(localNativeLibrariesDirectory, file);
            File destFile = new File(destinationDirectory, relativePath);
            if (destFile.exists()) {
                String orgFileMd5 = MD5Util.getFileMD5(file);
                String destFileMd5 = MD5Util.getFileMD5(destFile);
                if (!orgFileMd5.equals(destFileMd5)) {
                    dumpFiles.add(file.getAbsolutePath() + " to " + destFile.getAbsolutePath());
                }
            }
        }
        if (dumpFiles.size() > 0) {
            throw new RuntimeException("Copy native so error,duplicate file exist!:\n" + StringUtils.join(dumpFiles, "\n"));
        }
        FileUtils.copyDirectory(localNativeLibrariesDirectory, destinationDirectory, filter);
    } catch (IOException e) {
        throw new RuntimeException("Could not copy native dependency.", e);
    }
}
Also used : IOFileFilter(org.apache.commons.io.filefilter.IOFileFilter) IOException(java.io.IOException) File(java.io.File)

Example 4 with IOFileFilter

use of org.apache.commons.io.filefilter.IOFileFilter in project jmeter by apache.

the class Save method createBackupFile.

/**
     * <p>
     * Create a backup copy of the specified file whose name will be
     * <code>{baseName}-{version}.jmx</code><br>
     * Where :<br>
     * <code>{baseName}</code> is the name of the file to backup without its
     * <code>.jmx</code> extension. For a file named <code>testplan.jmx</code>
     * it would then be <code>testplan</code><br>
     * <code>{version}</code> is the version number automatically incremented
     * after the higher version number of pre-existing backup files. <br>
     * <br>
     * Example: <code>testplan-000028.jmx</code> <br>
     * <br>
     * If <code>jmeter.gui.action.save.backup_directory</code> is <b>not</b>
     * set, then backup files will be created in
     * <code>${JMETER_HOME}/backups</code>
     * </p>
     * <p>
     * Backup process is controlled by the following jmeter/user properties :<br>
     * <table border=1>
     * <tr>
     * <th align=left>Property</th>
     * <th align=left>Type/Value</th>
     * <th align=left>Description</th>
     * </tr>
     * <tr>
     * <td><code>jmeter.gui.action.save.backup_on_save</code></td>
     * <td><code>true|false</code></td>
     * <td>Enables / Disables backup</td>
     * </tr>
     * <tr>
     * <td><code>jmeter.gui.action.save.backup_directory</code></td>
     * <td><code>/path/to/backup/directory</code></td>
     * <td>Set the directory path where backups will be stored upon save. If not
     * set then backups will be created in <code>${JMETER_HOME}/backups</code><br>
     * If that directory does not exist, it will be created</td>
     * </tr>
     * <tr>
     * <td><code>jmeter.gui.action.save.keep_backup_max_hours</code></td>
     * <td><code>integer</code></td>
     * <td>Maximum number of hours to preserve backup files. Backup files whose
     * age exceeds that limit should be deleted and will be added to this method
     * returned list</td>
     * </tr>
     * <tr>
     * <td><code>jmeter.gui.action.save.keep_backup_max_count</code></td>
     * <td><code>integer</code></td>
     * <td>Max number of backup files to be preserved. Exceeding backup files
     * should be deleted and will be added to this method returned list. Only
     * the most recent files will be preserved.</td>
     * </tr>
     * </table>
     * </p>
     * 
     * @param fileToBackup
     *            The file to create a backup from
     * @return A list of expired backup files selected according to the above
     *         properties and that should be deleted after the save operation
     *         has performed successfully
     */
private List<File> createBackupFile(File fileToBackup) {
    if (!BACKUP_ENABLED || !fileToBackup.exists()) {
        return EMPTY_FILE_LIST;
    }
    //$NON-NLS-1$
    char versionSeparator = '-';
    String baseName = fileToBackup.getName();
    // remove .jmx extension if any
    baseName = baseName.endsWith(JMX_FILE_EXTENSION) ? baseName.substring(0, baseName.length() - JMX_FILE_EXTENSION.length()) : baseName;
    // get a file to the backup directory
    File backupDir = new File(BACKUP_DIRECTORY);
    backupDir.mkdirs();
    if (!backupDir.isDirectory()) {
        log.error(//$NON-NLS-1$
        "Could not backup file ! Backup directory does not exist, is not a directory or could not be created ! <{}>", //$NON-NLS-2$
        backupDir.getAbsolutePath());
    }
    /**
         *  select files matching
         * {baseName}{versionSeparator}{version}{jmxExtension}
         * where {version} is a 6 digits number
         */
    //$NON-NLS-1$
    String backupPatternRegex = Pattern.quote(baseName + versionSeparator) + "([\\d]{6})" + Pattern.quote(JMX_FILE_EXTENSION);
    Pattern backupPattern = Pattern.compile(backupPatternRegex);
    // create a file filter that select files matching a given regex pattern
    IOFileFilter patternFileFilter = new PrivatePatternFileFilter(backupPattern);
    // get all backup files in the backup directory
    List<File> backupFiles = new ArrayList<>(FileUtils.listFiles(backupDir, patternFileFilter, null));
    // find the highest version number among existing backup files (this
    // should be the more recent backup)
    int lastVersionNumber = 0;
    for (File backupFile : backupFiles) {
        Matcher matcher = backupPattern.matcher(backupFile.getName());
        if (matcher.find() && matcher.groupCount() > 0) {
            // parse version number from the backup file name
            // should never fail as it matches the regex
            int version = Integer.parseInt(matcher.group(1));
            lastVersionNumber = Math.max(lastVersionNumber, version);
        }
    }
    // find expired backup files
    List<File> expiredFiles = new ArrayList<>();
    if (BACKUP_MAX_HOURS > 0) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.HOUR_OF_DAY, -BACKUP_MAX_HOURS);
        long expiryDate = cal.getTime().getTime();
        // select expired files that should be deleted
        IOFileFilter expiredFileFilter = FileFilterUtils.ageFileFilter(expiryDate, true);
        expiredFiles.addAll(FileFilterUtils.filterList(expiredFileFilter, backupFiles));
    }
    // sort backups from by their last modified time
    Collections.sort(backupFiles, (o1, o2) -> {
        long diff = o1.lastModified() - o2.lastModified();
        // contract
        return diff < 0 ? -1 : diff > 0 ? 1 : 0;
    });
    /**
         *  backup name is of the form 
         * {baseName}{versionSeparator}{version}{jmxExtension}
         */
    String backupName = baseName + versionSeparator + BACKUP_VERSION_FORMATER.format(lastVersionNumber + 1L) + JMX_FILE_EXTENSION;
    File backupFile = new File(backupDir, backupName);
    // create file backup
    try {
        FileUtils.copyFile(fileToBackup, backupFile);
    } catch (IOException e) {
        //$NON-NLS-1$
        log.error("Failed to backup file : {}", fileToBackup.getAbsolutePath(), e);
        return EMPTY_FILE_LIST;
    }
    // add the fresh new backup file (list is still sorted here)
    backupFiles.add(backupFile);
    // than required
    if (BACKUP_MAX_COUNT > 0 && backupFiles.size() > BACKUP_MAX_COUNT) {
        // keep the most recent files in the limit of the specified max
        // count
        expiredFiles.addAll(backupFiles.subList(0, backupFiles.size() - BACKUP_MAX_COUNT));
    }
    return expiredFiles;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IOFileFilter(org.apache.commons.io.filefilter.IOFileFilter) File(java.io.File)

Example 5 with IOFileFilter

use of org.apache.commons.io.filefilter.IOFileFilter in project java-in-action by xinghalo.

the class FileUtilsTest method FileOperation.

@Test
public void FileOperation() throws IOException {
    // 创建文件,如果文件存在则更新时间;如果不存在,创建一个空文件
    // 创建空文件的方式为:
    // final OutputStream out = openOutputStream(file);
    // IOUtils.closeQuietly(out);
    FileUtils.touch(new File("D://test/test4"));
    // 文件内容的对比
    FileUtils.contentEquals(new File("D://test/test1"), new File("D://test/test2"));
    // 忽略换行符,第三个参数是字符集
    FileUtils.contentEqualsIgnoreEOL(new File("D://test/test1"), new File("D://test/test2"), null);
    // 根据URL获取文件
    FileUtils.toFile(new URL("file://D://test/test1"));
    FileUtils.toFiles(null);
    FileUtils.toURLs(new File[] { new File("D://test/test1") });
    // 文件拷贝
    // 第三个参数是否更新时间
    FileUtils.copyFileToDirectory(new File("/test1"), new File("/dir"), true);
    // TODO 阅读doCopy源码
    FileUtils.copyFile(new File("/source"), new File("/target"), true);
    // 目录拷贝
    File srcDir = new File("/source");
    File destDir = new File("/target");
    FileUtils.copyDirectoryToDirectory(new File("/source"), new File("/target"));
    FileUtils.copyDirectory(new File("/source"), new File("/target"));
    // 仅仅拷贝目录
    FileUtils.copyDirectory(srcDir, destDir, DirectoryFileFilter.DIRECTORY);
    // 创建.txt过滤器
    IOFileFilter txtSuffixFilter = FileFilterUtils.suffixFileFilter(".txt");
    IOFileFilter txtFiles = FileFilterUtils.andFileFilter(FileFileFilter.FILE, txtSuffixFilter);
    // 创建包含目录或者txt文件的过滤器
    FileFilter filter = FileFilterUtils.orFileFilter(DirectoryFileFilter.DIRECTORY, txtFiles);
    // Copy using the filter
    // TODO 阅读doCopyDirectory源码
    FileUtils.copyDirectory(srcDir, destDir, filter);
    // 文件拷贝
    FileUtils.copyInputStreamToFile(new FileInputStream("/test"), new File("/test"));
    FileUtils.copyURLToFile(new URL("file:/test"), new File("/test"));
    // 删除文件
    // 删除目录下所有的内容
    FileUtils.deleteDirectory(new File("/test"));
    // 如果是目录,会级联删除;不会抛出异常
    FileUtils.deleteQuietly(new File("/test"));
    // 判断文件是否存在
    FileUtils.directoryContains(new File("/dir"), new File("/file"));
    // 清除目录中的内容,不会删除该目录;
    // 先verifiedListFiles检查目录,检查目录是否为目录、是否存在,然后调用listFiles,如果返回null,则抛出异常
    // 遍历目录中的文件,如果是目录则递归删除;如果是文件则强制删除,删除失败(文件不存在或无法删除)都会抛出异常
    // TODO verifiedListFiles  <------2016/11/24
    FileUtils.cleanDirectory(new File("/dir"));
    // 等待一个文件xx秒,知道文件创建后才返回。每max(100,remainning)循环检查一次
    while (FileUtils.waitFor(new File("/dir"), 60)) {
    }
    // 读取目标文件,内部调用IOUtils.toString(inputstream,"utf-8")
    String str = FileUtils.readFileToString(new File("/dir"), "utf-8");
    // 内部调用IOUtils.toByteArray(in)
    byte[] bytes = FileUtils.readFileToByteArray(new File("/dir"));
    // 内部调用IOUtils.readLines(in, Charsets.toCharset(encoding));
    List<String> strs = FileUtils.readLines(new File("/dir"), "utf-8");
    // 内部调用IOUtils.lineIterator(in, encoding)
    FileUtils.lineIterator(new File("/dir"), "utf-8");
    // 四个参数分别为:目标文件,写入的字符串,字符集,是否追加
    FileUtils.writeStringToFile(new File("/target"), "string", "utf-8", true);
    // write可以接受charsequence类型的数据,string,stringbuilder和stringbuffer都是实现了charsequence接口
    FileUtils.write(new File("/target"), "target char sequence", "utf-8", true);
    // (file,字符数组)
    FileUtils.writeByteArrayToFile(new File("/target"), "bytes".getBytes());
    // (file,字符数组,是否追加)
    FileUtils.writeByteArrayToFile(new File("/target"), "bytes".getBytes(), true);
    // (file,字符数组,起始位置,结束位置)
    FileUtils.writeByteArrayToFile(new File("/target"), "bytes".getBytes(), 0, 10);
    // (file,字符数组,起始位置,结束位置,是否追加)
    FileUtils.writeByteArrayToFile(new File("/target"), "bytes".getBytes(), 0, 10, true);
    // writeLines多了一个lineEnding参数
    FileUtils.writeLines(new File("/target"), "utf-8", FileUtils.readLines(new File("/target"), "utf-8"));
    // 强制删除
    FileUtils.forceDelete(new File("/target"));
    // 在JVM
    FileUtils.forceDeleteOnExit(new File("/target"));
    // 强制创建文件目录,如果文件存在,会抛出异常
    FileUtils.forceMkdir(new File("/target"));
    // 强制创建父级目录
    FileUtils.forceMkdirParent(new File("/xxxx/target"));
    // 如果是文件,直接读取文件大小;如果是目录,级联计算文件下的所有文件大小
    // 返回Long
    FileUtils.sizeOf(new File("/target"));
    // 返回BigInteger
    FileUtils.sizeOfAsBigInteger(new File("/target"));
    FileUtils.sizeOfDirectory(new File("/target"));
    FileUtils.sizeOfDirectoryAsBigInteger(new File("/target"));
    // 对比文件新旧
    FileUtils.isFileNewer(new File("/target"), new File("/file"));
    FileUtils.isFileOlder(new File("/target"), new Date());
    FileUtils.checksum(new File("/target"), new CRC32());
    FileUtils.checksumCRC32(new File("/target"));
    FileUtils.moveDirectory(new File("/target"), new File("/file"));
    FileUtils.moveDirectoryToDirectory(new File("/target"), new File("/file"), true);
    FileUtils.moveFile(new File("/target"), new File("/file"));
    FileUtils.moveFileToDirectory(new File("/target"), new File("/dir"), true);
    FileUtils.moveToDirectory(new File("/target"), new File("/dir"), true);
    FileUtils.isSymlink(new File("/target"));
}
Also used : CRC32(java.util.zip.CRC32) IOFileFilter(org.apache.commons.io.filefilter.IOFileFilter) IOFileFilter(org.apache.commons.io.filefilter.IOFileFilter) FileFileFilter(org.apache.commons.io.filefilter.FileFileFilter) DirectoryFileFilter(org.apache.commons.io.filefilter.DirectoryFileFilter) URL(java.net.URL) Date(java.util.Date) Test(org.junit.Test)

Aggregations

IOFileFilter (org.apache.commons.io.filefilter.IOFileFilter)21 File (java.io.File)16 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 URL (java.net.URL)3 SuffixFileFilter (org.apache.commons.io.filefilter.SuffixFileFilter)3 WildcardFileFilter (org.apache.commons.io.filefilter.WildcardFileFilter)3 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 Matcher (java.util.regex.Matcher)2 DirectoryFileFilter (org.apache.commons.io.filefilter.DirectoryFileFilter)2 Tuple (org.jowidgets.util.Tuple)2 Test (org.junit.Test)2 HashMultimap (com.google.common.collect.HashMultimap)1 Multimap (com.google.common.collect.Multimap)1 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1