视频地址:http://list.youku.com/albumlist/show/id_52130068
视频地址:https://member.bilibili.com/v2#/upload-manager/article?page=1
01压缩特定位置的文件到指定的文件夹(指定压缩后文件名)
package cn.itcast.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* Created by PC on 2019/3/18.
*/
public class ZipFileMgr {
public static void main(String[] args) {
//原文件路径(将要压缩的文件路径)
String sourcePath = "E:\\CODE\\hello.html";
//输出文件的路径
String outPath = "E:\\CODE\\test\\hello.zip";
try {
//文件输出流
FileOutputStream fout = new FileOutputStream(outPath);
//zip格式的输出流
ZipOutputStream zout = new ZipOutputStream(fout);
//将一个原文件写入到一个压缩文件中
File sourceFile = new File(sourcePath);
//压缩条目
String zipEntryName = sourceFile.getName();
//将一个将要压缩的文件写入到压缩条目中
zout.putNextEntry(new ZipEntry(zipEntryName));
//读入将要压缩的文件
FileInputStream fin = new FileInputStream(sourceFile);
byte[] buff =new byte[1024];
int length;
//将原文件写入到zip格式输出流
while((length = fin.read(buff))>0)
{
zout.write(buff,0,length);
}
fin.close();
zout.closeEntry();
zout.close();
fout.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
提取压缩文件的方法后形成的代码
package cn.itcast.util;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* Created by PC on 2019/3/18.
*/
public class ZipFileMgr {
public static void main(String[] args) {
//原文件路径(将要压缩的文件)
String sourcePath = "E:\\CODE\\一个程序员的自我修养.txt";
//输出文件的路径
String outPath = "E:\\CODE\\test\\一个程序员的自我修养.zip";
try {
//文件输出流
FileOutputStream fout = new FileOutputStream(outPath);
//zip格式的输出流
ZipOutputStream zout = new ZipOutputStream(fout);
//将一个原文件写入到一个压缩文件中
File sourceFile = new File(sourcePath);
//压缩条目
String zipEntryName = sourceFile.getName();
//压缩文件
zipFile(zout, sourceFile, zipEntryName);
zout.close();
fout.close();
}catch (Exception e){
e.printStackTrace();
}
}
/**
*
* @param zout zip格式的文件输出流
* @param sourceFile 原文件路径(将要压缩的文件)
* @param zipEntryName 压缩条目(实际上就是文件的相对路径)
* @throws IOException
*/
private static void zipFile(ZipOutputStream zout, File sourceFile, String zipEntryName) throws IOException {
//将一个将要压缩的文件写入到压缩条目中
zout.putNextEntry(new ZipEntry(zipEntryName));
//读入将要压缩的文件
FileInputStream fin = new FileInputStream(sourceFile);
byte[] buff =new byte[1024];
int length;
//将原文件写入到zip格式输出流
while((length = fin.read(buff))>0)
{
zout.write(buff,0,length);
}
fin.close();
zout.closeEntry();
}
}
02压缩文件夹(纯文件夹,即文件夹中不含有其他新的子文件夹)
package cn.itcast.util;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* Created by PC on 2019/3/18.
*/
public class ZipFileMgr {
public static void main(String[] args) {
//原文件路径(将要压缩的文件)
String sourcePath = "E:\\CODE\\MFC";
//输出文件的路径
String outPath = "E:\\CODE\\test\\MFC.zip";
try {
//文件输出流
FileOutputStream fout = new FileOutputStream(outPath);
//zip格式的输出流
ZipOutputStream zout = new ZipOutputStream(fout);
//将一个原文件写入到一个压缩文件中
File sourceFile = new File(sourcePath);
//压缩条目
String zipEntryName = sourceFile.getName();
//压缩文件
//zipFile(zout, sourceFile, zipEntryName);
//压缩目录,遍历目录里面的所有文件(当且仅当文件夹只有文件才能压缩,不含有其他文件夹)
for(File file:sourceFile.listFiles()){
zipFile(zout,file,zipEntryName+"/"+file.getName());
}
zout.close();
fout.close();
}catch (Exception e){
e.printStackTrace();
}
}
/**
*
* @param zout zip格式的文件输出流
* @param sourceFile 原文件路径(将要压缩的文件)
* @param zipEntryName 压缩条目(实际上就是文件的相对路径)
* @throws IOException
*/
private static void zipFile(ZipOutputStream zout, File sourceFile, String zipEntryName) throws IOException {
//将一个将要压缩的文件写入到压缩条目中
zout.putNextEntry(new ZipEntry(zipEntryName));
//读入将要压缩的文件
FileInputStream fin = new FileInputStream(sourceFile);
byte[] buff =new byte[1024];
int length;
//将原文件写入到zip格式输出流
while((length = fin.read(buff))>0)
{
zout.write(buff,0,length);
}
fin.close();
zout.closeEntry();
}
}
03压缩文件夹(包含文件夹中含有子文件夹和空文件夹的情况) 和文件
package cn.itcast.util;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* Created by PC on 2019/3/18.
*/
public class ZipFileMgr {
public static void main(String[] args) {
//原文件路径(将要压缩的文件)
String sourcePath = "E:\\CODE\\一个程序员的自我修养.txt";
//输出文件的路径
String outPath = "E:\\CODE\\test\\一个程序员的自我修养2.zip";
zip(sourcePath, outPath);
}
/**
* 压缩文件或者文件夹
* @param sourcePath (将要压缩的文件)
* @param outPath 输出文件的路径
*/
private static void zip(String sourcePath, String outPath) {
try {
//文件输出流
FileOutputStream fout = new FileOutputStream(outPath);
//zip格式的输出流
ZipOutputStream zout = new ZipOutputStream(fout);
//将一个原文件写入到一个压缩文件中
File sourceFile = new File(sourcePath);
//压缩条目
String zipEntryName = sourceFile.getName();
if(sourceFile.isFile()) {
//压缩文件
zipFile(zout, sourceFile, zipEntryName);
}else {
//压缩目录
zipDirectory(zout, sourceFile, zipEntryName);
}
zout.close();
fout.close();
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 压缩文件夹
* @param zout zip格式的文件输出流
* @param sourceFile 原文件路径(将要压缩的文件夹)
* @param zipEntryName 压缩条目(实际上就是一个文件的相对路径)
* @throws IOException
*/
private static void zipDirectory(ZipOutputStream zout, File sourceFile, String zipEntryName) throws IOException {
//压缩目录,遍历目录里面的所有文件(当且仅当文件夹只有文件才能压缩,不含有其他文件夹)
for(File file:sourceFile.listFiles()){
if(file.isFile()) { //如果是文件,则直接调用压缩文件的方法进行压缩
zipFile(zout, file, zipEntryName + "/" + file.getName());
}else{
//说明现在的file是目录,则需要将该目录的所有文件压缩
if (file.listFiles().length>0) { //非空文件夹
//递归调用压缩子文件夹的方法
zipDirectory(zout, file, zipEntryName + "/" + file.getName()); //内容是: MFC/新建文件夹
}else{
//空文件夹
//将压缩条目写入到压缩对象中
zout.putNextEntry(new ZipEntry(zipEntryName + "/" + file.getName()+"/")); //最后添加斜线(通知解释器知道这是目录)
zout.closeEntry();
}
}
}
}
/**
*
* @param zout zip格式的文件输出流
* @param sourceFile 原文件路径(将要压缩的文件)
* @param zipEntryName 压缩条目(实际上就是文件的相对路径)
* @throws IOException
*/
private static void zipFile(ZipOutputStream zout, File sourceFile, String zipEntryName) throws IOException {
//将一个将要压缩的文件写入到压缩条目中
zout.putNextEntry(new ZipEntry(zipEntryName));
//读入将要压缩的文件
FileInputStream fin = new FileInputStream(sourceFile);
byte[] buff =new byte[1024];
int length;
//将原文件写入到zip格式输出流
while((length = fin.read(buff))>0)
{
zout.write(buff,0,length);
}
fin.close();
zout.closeEntry();
}
}
04解压缩文件和文件夹
package cn.itcast.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
* Created by PC on 2019/3/18.
*/
public class UnZipFileMgr {
public static void main(String[] args) {
//原文件(将解压文件的路径)
String sourcePath = "E:\\CODE\\test\\MFC4.zip";
//输出文件路径
String outPath = "E:\\CODE\\test2";
upZip(sourcePath, outPath);
}
/**
* 解压文件或文件夹
* @param sourcePath 原文件(待压缩文件的路径)
* @param outPath 输出文件路径
*/
private static void upZip(String sourcePath, String outPath) {
try {
//文件输入流
FileInputStream fin = new FileInputStream(sourcePath);
//zip格式的输入流
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry zipEntry;
//遍历压缩文件中的所有压缩条目
while((zipEntry = zin.getNextEntry())!= null) {
//将压缩条目输出到输出路径
File targetFile = new File(outPath + File.separator + zipEntry.getName());
//判断目标输出文件的父目录是否存在;如果不存在的话则需要创建一个父文件夹
if (!targetFile.getParentFile().exists()) {
//不存在父文件夹
targetFile.getParentFile().mkdirs();
}
if (zipEntry.isDirectory()) {
//判断当前的压缩条目是否是文件夹,如果是文件夹的话,直接在输出路径创建文件即可
targetFile.mkdirs();
} else {
//当前的压缩条目是一个文件,需要将输入的压缩文件内容输出到输出路径
FileOutputStream fout = new FileOutputStream(targetFile);
byte[] buff = new byte[1024];
int length;
while ((length = zin.read(buff)) > 0) {
fout.write(buff, 0, length);
}
fout.close();
}
}
zin.close();
fin.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
05校验和算法(使用CRS32或者Adler32)
//压缩
package cn.itcast.util;
import java.io.*;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* Created by PC on 2019/3/18.
*/
public class ZipFileMgr {
public static void main(String[] args) {
//原文件路径(将要压缩的文件)
String sourcePath = "E:\\CODE\\一个程序员的自我修养.txt";
//输出文件的路径
String outPath = "E:\\CODE\\test\\一个程序员的自我修养2.zip";
zip(sourcePath, outPath);
}
/**
* 压缩文件或者文件夹
* @param sourcePath (将要压缩的文件)
* @param outPath 输出文件的路径
*/
private static void zip(String sourcePath, String outPath) {
try {
//文件输出流
FileOutputStream fout = new FileOutputStream(outPath);
//写入数据校验和的输出流,校验和可用于校验输出数据的完整性
CheckedOutputStream checkedOutputStream = new CheckedOutputStream(fout, new CRC32());
//zip格式的输出流
ZipOutputStream zout = new ZipOutputStream(checkedOutputStream); //校验输出流传入
//将一个原文件写入到一个压缩文件中
File sourceFile = new File(sourcePath);
//压缩条目
String zipEntryName = sourceFile.getName();
if(sourceFile.isFile()) {
//压缩文件
zipFile(zout, sourceFile, zipEntryName);
}else {
//压缩目录
zipDirectory(zout, sourceFile, zipEntryName);
}
zout.close();
System.out.println("校验和为:"+checkedOutputStream.getChecksum().getValue());
checkedOutputStream.close();
fout.close();
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 压缩文件夹
* @param zout zip格式的文件输出流
* @param sourceFile 原文件路径(将要压缩的文件夹)
* @param zipEntryName 压缩条目(实际上就是一个文件的相对路径)
* @throws IOException
*/
private static void zipDirectory(ZipOutputStream zout, File sourceFile, String zipEntryName) throws IOException {
//压缩目录,遍历目录里面的所有文件(当且仅当文件夹只有文件才能压缩,不含有其他文件夹)
for(File file:sourceFile.listFiles()){
if(file.isFile()) { //如果是文件,则直接调用压缩文件的方法进行压缩
zipFile(zout, file, zipEntryName + "/" + file.getName());
}else{
//说明现在的file是目录,则需要将该目录的所有文件压缩
if (file.listFiles().length>0) { //非空文件夹
//递归调用压缩子文件夹的方法
zipDirectory(zout, file, zipEntryName + "/" + file.getName()); //内容是: MFC/新建文件夹
}else{
//空文件夹
//将压缩条目写入到压缩对象中
zout.putNextEntry(new ZipEntry(zipEntryName + "/" + file.getName()+"/")); //最后添加斜线(通知解释器知道这是目录)
zout.closeEntry();
}
}
}
}
/**
*
* @param zout zip格式的文件输出流
* @param sourceFile 原文件路径(将要压缩的文件)
* @param zipEntryName 压缩条目(实际上就是文件的相对路径)
* @throws IOException
*/
private static void zipFile(ZipOutputStream zout, File sourceFile, String zipEntryName) throws IOException {
//将一个将要压缩的文件写入到压缩条目中
zout.putNextEntry(new ZipEntry(zipEntryName));
//读入将要压缩的文件
FileInputStream fin = new FileInputStream(sourceFile);
byte[] buff =new byte[1024];
int length;
//将原文件写入到zip格式输出流
while((length = fin.read(buff))>0)
{
zout.write(buff,0,length);
}
fin.close();
zout.closeEntry();
}
}
//解压
package cn.itcast.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
* Created by PC on 2019/3/18.
*/
public class UnZipFileMgr {
public static void main(String[] args) {
//原文件(将解压文件的路径)
String sourcePath = "E:\\CODE\\test\\一个程序员的自我修养2.zip";
//输出文件路径
String outPath = "E:\\CODE\\test2";
upZip(sourcePath, outPath);
}
/**
* 解压文件或文件夹
* @param sourcePath 原文件(待压缩文件的路径)
* @param outPath 输出文件路径
*/
private static void upZip(String sourcePath, String outPath) {
try {
//文件输入流
FileInputStream fin = new FileInputStream(sourcePath);
//所读取数据校验和的输入流,校验和可用于验证输入数据的完整性
CheckedInputStream checkedInputStream = new CheckedInputStream(fin, new CRC32());
//zip格式的输入流
ZipInputStream zin = new ZipInputStream(checkedInputStream); //校验输入流传入
ZipEntry zipEntry;
//遍历压缩文件中的所有压缩条目
while((zipEntry = zin.getNextEntry())!= null) {
//将压缩条目输出到输出路径
File targetFile = new File(outPath + File.separator + zipEntry.getName());
//判断目标输出文件的父目录是否存在;如果不存在的话则需要创建一个父文件夹
if (!targetFile.getParentFile().exists()) {
//不存在父文件夹
targetFile.getParentFile().mkdirs();
}
if (zipEntry.isDirectory()) {
//判断当前的压缩条目是否是文件夹,如果是文件夹的话,直接在输出路径创建文件即可
targetFile.mkdirs();
} else {
//当前的压缩条目是一个文件,需要将输入的压缩文件内容输出到输出路径
FileOutputStream fout = new FileOutputStream(targetFile);
byte[] buff = new byte[1024];
int length;
while ((length = zin.read(buff)) > 0) {
fout.write(buff, 0, length);
}
fout.close();
}
}
zin.close();
System.out.println("校验和为:"+checkedInputStream.getChecksum().getValue());
checkedInputStream.close();
fin.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}