您的位置:首页 > 编程语言 > Java开发

FIle 文件操作,包括文件的复制、删除以及zip和rar格式的解压操作

2016-07-11 18:38 567 查看
由于rar压缩算法不公开,所以java api中没有rar的api,做使用rar之前必须使用开源jar包

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.zip.ZipEntry;

import java.util.zip.ZipException;

import java.util.zip.ZipInputStream;

import com.github.junrar.Archive;

import com.github.junrar.exception.RarException;

import com.github.junrar.rarfile.FileHeader;

/**

 * 文件操作类

 * @author lintian

 * @since 2016-07-11

 */

public class FileOperateUtil {

    /**

     * 删除文件

     * @param basePath 文件路径

     */

    public static void deleteFiles(String basePath){

        File file=new File(basePath);

        deleteAllFiles(file);

        

    }

    /**

     * 删除所有文件

     * @param file

     */

    private static void deleteAllFiles(File file){

        if(file.exists()){

            if(file.isDirectory()){

                File[] tempFiles=file.listFiles();

                for(int i=0,len=tempFiles.length;i<len;i++){

                    if(tempFiles[i].isDirectory()){

                        deleteAllFiles(tempFiles[i]);

                        tempFiles[i].delete();

                    }else{

                        tempFiles[i].delete();

                    }

                }

            }else{

                file.delete();

            }

        }

    }

    /**

     * 从一个文件夹中复制所有文件数据到另一个文件夹中

     * @param fromPath

     * @param toPath

     * @throws IOException

     */

    public static void copyDirectiory(String fromPath,String toPath) throws IOException{

        deleteFiles(toPath);//复制到前段服务器之前先把服务器中的原始数据清空

        File fromFile=new File(fromPath);

        File toFile=new File(toPath);

        if(!toFile.exists()){//第一次判断是否存在站点根目录文件夹

            toFile.mkdir();

        }

        if(fromFile.exists()){

            if(fromFile.isDirectory()){

                File[] fromFileLists=fromFile.listFiles();

                for(int i=0,len=fromFileLists.length;i<len;i++){

                    if(fromFileLists[i].isDirectory()){

                        String orginPath=fromFileLists[i].getPath();

                        String targetPath=toFile.getPath()+File.separator+fromFileLists[i].getName();

                        copyDirectiory(orginPath, targetPath);

                    }else{

                        File targetFile=new File(toFile.getPath()+File.separator+fromFileLists[i].getName());

                        copyFile(fromFileLists[i],targetFile);

                    }

                }

            }

        }

    }

    

    /**

     * 复制文件

     * @param fromFile

     * @param toFile

     * @throws IOException

     */

    private static void copyFile(File fromFile,File toFile) throws IOException{

        BufferedInputStream bufferedInput=null;

        BufferedOutputStream bufferedOutput=null;

        FileInputStream fileInput=null;

        FileOutputStream fileOutput=null;

        fileInput=new FileInputStream(fromFile);

        fileOutput=new FileOutputStream(toFile);

        bufferedInput=new BufferedInputStream(fileInput);

        bufferedOutput=new BufferedOutputStream(fileOutput);

        byte data[]=new byte[1024];

        int count=0;

        while((count=fileInput.read(data,0,data.length))!=-1){

            fileOutput.write(data, 0,count);

        }

        bufferedOutput.flush();

        bufferedInput.close();

        bufferedOutput.close();

        fileOutput.close();

        fileInput.close();

        

    }

    /**

     * 解压文件

     * @param file

     * @throws IOException

     * @throws FileNotFoundException

     * @throws RarException

     */

    public static void deCompress(String filePath) throws FileNotFoundException, IOException, RarException{

        String ext=filePath.substring(filePath.lastIndexOf(".")+1, filePath.length());

        if("zip".equals(ext.trim())){

            zipDeCompress(filePath);

            deleteFiles(filePath);

        }else if("rar".equals(ext.trim())){

            rarDeCompress(filePath);

            deleteFiles(filePath);

        }else{

            throw new IOException("解压文件的格式不属于zip和rar");

        }

    

    }

    

    /**

     * 解压zip压缩文件,zip压缩文件中不能含有中文文件名

     * @param filePath

     * @throws ZipException

     * @throws IOException

     */

    private static void zipDeCompress(String filePath) throws ZipException, IOException{

        File file=new File(filePath);

        ZipEntry zEntry=null;

        ZipInputStream zipIn = new ZipInputStream(new FileInputStream(file));

        while((zEntry=zipIn.getNextEntry())!=null){

            if(zEntry.isDirectory()){

                String fileName=file.getPath().substring(0, file.getPath().lastIndexOf(File.separator))+File.separator+zEntry.getName();

                File fileDir=new File(fileName);

                if(!fileDir.exists()){

                    fileDir.mkdir();

                }

            }else{

                String    fileName=file.getPath().substring(0, file.getPath().lastIndexOf(File.separator))+File.separator+zEntry.getName();

                File targetFile=new File(fileName);

                FileOutputStream fout=new FileOutputStream(targetFile);

                byte data[]=new byte[1024];

                int count=0;

                while((count=zipIn.read(data,0,1024))!=-1){

                    fout.write(data, 0, count);

                }

                

                fout.flush();

                fout.close();

                

            }

        }

        zipIn.close();

    }

    /**

     * rar格式压缩文件解压

     * @param filePath

     * @throws RarException

     * @throws IOException

     */

    private static void rarDeCompress(String filePath) throws RarException, IOException {

        File file=new File(filePath);

        Archive archive = null;

        FileOutputStream unOut = null;

        archive = new Archive(file);

        FileHeader fileHeader ;

        while(( fileHeader = archive.nextFileHeader())!=null){

            if(fileHeader.isDirectory()){

                String fileName=file.getPath().substring(0, file.getPath().lastIndexOf(File.separator))+File.separator+fileHeader.getFileNameString();

                File fileDir=new File(fileName);

                if(!fileDir.exists()){

                    fileDir.mkdir();

                }

            }else{

                String    fileName=file.getPath().substring(0, file.getPath().lastIndexOf(File.separator))+File.separator+fileHeader.getFileNameString();

                File dir = new File(fileName.substring(0, fileName.lastIndexOf(File.separator)));

                if (!dir.exists() || !dir.isDirectory()) {

                    dir.mkdirs();

                }

                File targetFile=new File(fileName);

                unOut=new FileOutputStream(targetFile);

                archive.extractFile(fileHeader, unOut);

                unOut.flush();

                unOut.close();

            }

        }

        archive.close();

    }

    

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Java file 压缩 解压