Java脚本批量修改文件名、解压.gz文件

  • Post author:
  • Post category:java


public class Demo {

    public static void main(String[] args) throws IOException {
        File file = new File("C:\\xx\\xx\\xxpng");
        System.out.println(file.getParent());
        reName(file);

        // 解压 手机端 nginx下www下的.gz文件
        String filePath = "C:\\xxx\\xxx\\xxx\\xxx\\xxx";
        folderMethod2(filePath);
    }

    private static void reName(File f) {
        File[] fi = f.listFiles();
        String fileOldName = "";
        String fileName = "";
        for (File file : fi) {
            if(file.isFile()){
                fileOldName = file.getName();
                fileName = fileOldName.substring(0,fileOldName.indexOf("."));
                System.out.println(fileName);
                file.renameTo(new File("C:\\xx\\xx\\xx\\" + fileName + ".jpg"));
                file.delete();
            }
        }
    }

    public static void folderMethod2(String path) throws IOException {
        File file = new File(path);
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (null != files) {
                for (File file2 : files) {
                    if (file2.isDirectory()) {
                        folderMethod2(file2.getAbsolutePath());
                    } else {
                        System.out.println("文件:" + file2.getAbsolutePath());
                        doUncompressFile(file2.getAbsolutePath());
                    }
                }
            }
        } else {
            System.out.println("文件不存在!");
        }
    }

    /**
     * Uncompress the incoming file.
     * @param inFileName Name of the file to be uncompressed
     */
    private static void doUncompressFile(String inFileName) {
        System.out.println("解压.gz文件:" + inFileName);
        try {

            if (!inFileName.toLowerCase().endsWith(".gz")) {
                System.err.println("File name is not extension of \".gz\"");
                return;
            }

            System.out.println("Opening the compressed file.");
            GZIPInputStream in = null;
            try {
                in = new GZIPInputStream(new FileInputStream(inFileName));
            } catch(FileNotFoundException e) {
                System.err.println("File not found. " + inFileName);
                System.exit(1);
            }

            System.out.println("Open the output file.");
            String outFileName = getFileName(inFileName);
            FileOutputStream out = null;
            try {
                out = new FileOutputStream(outFileName);
            } catch (FileNotFoundException e) {
                System.err.println("Could not write to file. " + outFileName);
                System.exit(1);
            }

            System.out.println("Transfering bytes from compressed file to the output file.");
            byte[] buf = new byte[1024];
            int len;
            while((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }

            System.out.println("Closing the file and stream");
            in.close();
            out.close();
            new File(inFileName).delete();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }

    }

    /**
     * Used to extract and return the extension of a given file.
     * @param f Incoming file to get the extension of
     * @return <code>String</code> representing the extension of the incoming
     *         file.
     */
    public static String getExtension(String f) {
        String ext = "";
        int i = f.lastIndexOf('.');

        if (i > 0 &&  i < f.length() - 1) {
            ext = f.substring(i+1);
        }
        return ext;
    }

    /**
     * Used to extract the filename without its extension.
     * @param f Incoming file to get the filename
     * @return <code>String</code> representing the filename without its
     *         extension.
     */
    public static String getFileName(String f) {
        String fname = "";
        int i = f.lastIndexOf('.');

        if (i > 0 &&  i < f.length() - 1) {
            fname = f.substring(0,i);
        }
        return fname;
    }

}



版权声明:本文为chendi19881217原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。