您的位置:首页 > 其它

文件的压缩和解压

2016-11-08 14:34 155 查看
public void zipFile(){
String zipName = "E:/TestWork/zipName.zip";

byte[] buffer= new byte[1024];

try {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipName));
File[] f ={new File("E:/TestWork/1.jpg"),new File("E:/TestWork/2.mp4")};
for(int i=0;i<f.length;i++){
FileInputStream fis = new FileInputStream(f[i]);
out.putNextEntry(new ZipEntry(f[i].getName()));
int len =0;
while((len=fis.read(buffer))>0){
out.write(buffer,0,len);
}
out.closeEntry();
fis.close();
}
out.close();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}

}
/**

* @param zippath 压缩包的路径
* @param outzippath 制定的解压的文件路径
*/
public void unZipfiles(String zippath,String outzippath){

try {

            File file = new File(zippath);

            File outFile = null;

            ZipFile zipFile = new ZipFile(file);

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

            ZipEntry entry = null;

            InputStream input = null;

            OutputStream output = null;

            while((entry = zipInput.getNextEntry()) != null){

                System.out.println("解压缩" + entry.getName() + "文件");

                outFile = new File(outzippath + File.separator + entry.getName());

                if(!outFile.getParentFile().exists()){

                    outFile.getParentFile().mkdir();

                }

                if(!outFile.exists()){

                    outFile.createNewFile();

                }

                input = zipFile.getInputStream(entry);

                output = new FileOutputStream(outFile);

                int temp = 0;

                while((temp = input.read()) != -1){

                    output.write(temp);

                }

                input.close();

                output.close();

            }
} catch (Exception e) {
e.printStackTrace();
}

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