zip壓縮工具類
阿新 • • 發佈:2019-01-11
package com.test.common.util.zipencrypt;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.util.UUID;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import javax.crypto.Cipher;
import com.test.common.util.file.FileUtils;
/**
* 類描述:提供加密(RSA)壓縮功能服務
*
*
* @version v1.0.0 建立時間 2009-4-4 上午11:14:56
*/
public class ZipEncrypt {
private PrivateKey privateKey = null;
private PublicKey publicKey = null;
/**
* 解壓縮檔案.
*
* @param intputFilePath
* 要解壓縮檔案路徑及檔名
* @param outputFilePath
* 解壓縮檔案輸出路徑
* @return 0:成功 1:失敗+失敗原因 (例如:1:要解壓縮的檔案不存在)
*/
public String unzipFile(String intputFilePath, String outputFilePath) {
String result = "0";
boolean b = isExists(intputFilePath);
if (!b) {
return "1:要解壓縮的檔案不存在";
} else {
File file = new File(outputFilePath);
if (!file.exists()) {
file.mkdirs();
}
File outFile = new File(outputFilePath);
if (!outFile.exists()) {
outFile.mkdirs();
}
unZip(outputFilePath, intputFilePath);
}
return result;
}
/**
* 解壓縮檔案物件.
*
* @param intputFile
* 要解壓縮的檔案物件
* @param outputFilePath
* 解壓縮檔案輸出路徑
* @return 0:成功 1:失敗+失敗原因 (例如:1:要解壓縮的檔案格式錯誤)
*/
public String unzipFile(File intputFile, String outputFilePath) {
String result = "0";
if (intputFile == null) {
return "1:要解壓縮的檔案不存在";
}
String inFileName = intputFile.getName();
String endName = inFileName.split("\\.")[1];
if (!endName.equals("zip")) {
result = "1:解壓縮的檔案格式錯誤";
} else {
File file = new File(outputFilePath);
if (!file.exists()) {
file.mkdirs();
}
String infile = intputFile.getAbsolutePath();
unZip(outputFilePath, infile);
result = "0";
}
return result;
}
/**
* 壓縮檔案.
*
* @param intputFilePath
* 要壓縮檔案路徑及檔名
* @param outputFilePath
* 壓縮檔案輸出路徑(壓縮檔案的命名為:原檔名+.zip字尾)
* @return 0:成功 1:失敗+失敗原因 (例如:1:要壓縮的檔案不存在)
*/
public String zipFile(String intputFilePath, String outputFilePath) {
String result = "0";
boolean b = isExists(intputFilePath);
if (b) {
File file = new File(intputFilePath);
result = zipFile(file, outputFilePath);
} else {
result = "1:要壓縮的檔案不存在";
}
return result;
}
public String zipFile(String intputFilePath, String outputFilePath,
String name) {
String result = "0";
boolean b = isExists(intputFilePath);
if (b) {
File file = new File(intputFilePath);
result = zipFile(file, outputFilePath, name);
} else {
result = "1:要壓縮的檔案不存在";
}
return result;
}
/**
* 壓縮檔案物件.
*
* @param intputFile
* 要壓縮的檔案物件
* @param outputFilePath
* 壓縮檔案輸出路徑(壓縮檔案的命名為:原檔名+.zip字尾)
* @return 0:成功 1:失敗+失敗原因 (例如:1:要壓縮的檔案不存在)
*/
public String zipFile(File intputFile, String outputFilePath) {
String result = "0";
if (intputFile == null) {
result = "1:要壓縮的檔案不存在";
} else {
File outfile = new File(outputFilePath);
if (!outfile.exists()) {
outfile.mkdirs();
}
String fileName = intputFile.getName();
String tempFile = "/" + FileUtils.getFileNameWithoutType(fileName)
+ ".zip";
outputFilePath += tempFile;
String indirectory = intputFile.getAbsolutePath();
zip(indirectory, outputFilePath);
return result;
}
return result;
}
@SuppressWarnings("unused")
public String zipFile(File intputFile, String outputFilePath, String name) {
String result = "0";
if (intputFile == null) {
result = "1:要壓縮的檔案不存在";
} else {
File outfile = new File(outputFilePath);
if (!outfile.exists()) {
outfile.mkdirs();
}
String fileName = intputFile.getName();
String tempFile = name + ".zip";
outputFilePath += tempFile;
String indirectory = intputFile.getAbsolutePath();
zip(indirectory, outputFilePath);
return result;
}
return result;
}
/**
* 解密檔案.
*
* @param intputFilePath
* 要解密檔案路徑及檔名
* @param outputFilePath
* 解密檔案輸出路徑
* @param prikeyPath
* 私鑰檔案路徑
* @return 0:成功 1:失敗+失敗原因 (例如:1:要解密的檔案不存在)
*/
public String decryptFile(String intputFilePath, String outputFilePath,
String prikeyPath) {
String result = "0";
boolean b = isExists(intputFilePath);
if (!b) {
return "1:要解密的檔案不存在";
}
File file = new File(outputFilePath);
if (!file.exists()) {
file.mkdirs();
}
File temp = new File(intputFilePath);
outputFilePath += "/" + temp.getName();
try {
decrypt(intputFilePath, outputFilePath, getKey(prikeyPath));
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 解密檔案物件.
*
* @param intputFile
* 解密的檔案物件
* @param outputFilePath
* 解密檔案輸出路徑
* @param prikeyPath
* 私鑰檔案路徑
* @return 0:成功 1:失敗+失敗原因 (例如:1:要解密的檔案不存在)
*/
public String decryptFile(File intputFile, String outputFilePath,
String prikeyPath) {
String result = "0";
if (intputFile == null) {
return "1:要解密的檔案不存在";
}
File file = new File(outputFilePath);
if (!file.exists()) {
file.mkdirs();
}
String inFile = intputFile.getAbsolutePath();
String inName = intputFile.getName();
outputFilePath += "/" + inName;
try {
decrypt(inFile, outputFilePath, getKey(prikeyPath));
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 加密檔案.
*
* @param intputFilePath
* 要加密檔案路徑及檔名
* @param outputFilePath
* 加密檔案輸出路徑(加密檔案的命名為:原檔名)
* @param pubkeyPath
* 公鑰檔案路徑
* @return 0:成功 1:失敗+失敗原因 (例如:1:要加密的檔案不存在)
*/
public String encryptFile(String intputFilePath, String outputFilePath,
String pubkeyPath) {
String result = "0";
boolean b = isExists(intputFilePath);
if (!b) {
return "1:要加密的檔案不存在";
} else {
File file = new File(outputFilePath);
if (!file.exists()) {
file.mkdirs();
}
File tempfile = new File(intputFilePath);
outputFilePath += "/" + tempfile.getName();
try {
encrypt(intputFilePath, outputFilePath, getKey
(pubkeyPath));
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
/**
* 加密檔案物件.
*
* @param intputFile
* 要加密的檔案物件
* @param outputFilePath
* 加密檔案輸出路徑(加密檔案的命名為:原檔名)
* @param pubkeyPath
* 公鑰檔案路徑
* @return 0:成功 1:失敗+失敗原因 (例如:1:要加密的檔案不存在)
*/
public String encryptFile(File intputFile, String outputFilePath,
String pubkeyPath) {
String result = "0";
if (intputFile == null) {
return "1:要加密的檔案不存在";
} else {
File file = new File(outputFilePath);
if (!file.exists()) {
file.mkdirs();
}
String inpath = intputFile.getAbsolutePath();
outputFilePath += "/" + intputFile.getName();
try {
encrypt(inpath, outputFilePath, getKey(pubkeyPath));
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
/**
* 判斷檔案是否存在
*
* @param inputFilePath
* @return
*/
private boolean isExists(String inputFilePath) {
boolean b = false;
File tempfile = new File(inputFilePath);
if (tempfile.exists()) {
b = true;
}
return b;
}
/**
* 目錄壓縮
*
* @param out
* @param f
* @param base
* @throws Exception
*/
private void directoryZip(ZipOutputStream out, File f, String base)
throws Exception {
if (f.isDirectory()) {
File[] fl = f.listFiles();
out.putNextEntry(new ZipEntry(base + "/"));
if (base.length() == 0) {
base = "";
} else {
base = base + "/";
}
for (int i = 0; i < fl.length; i++) {
directoryZip(out, fl[i], base + fl[i].getName());
}
} else {
out.putNextEntry(new ZipEntry(base));
FileInputStream in = new FileInputStream(f);
byte[] bb = new byte[2048];
int aa = 0;
while ((aa = in.read(bb)) != -1) {
out.write(bb, 0, aa);
}
in.close();
}
}
/**
* 壓縮檔案
*
* @param zos
* @param file
* @throws Exception
*/
private void fileZip(ZipOutputStream zos, File file) throws Exception {
if (file.isFile()) {
zos.putNextEntry(new ZipEntry(file.getName()));
FileInputStream fis = new FileInputStream(file);
byte[] bb = new byte[2048];
int aa = 0;
while ((aa = fis.read(bb)) != -1) {
zos.write(bb, 0, aa);
}
fis.close();
} else {
directoryZip(zos, file, "");
}
}
/**
* 對directory目錄下的檔案壓縮,儲存為指定的檔案zipFile
*
* @param directory
* @param zipFile
*/
private void zip(String directory, String zipFile) {
try {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(
zipFile));
fileZip(zos, new File(directory));
zos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 壓縮byte陣列
*
* @param fileByte
* @return
*/
public byte[] zipByte(byte[] fileByte) {
byte[] temp = new byte[fileByte.length];
Deflater compresser = new Deflater();
compresser.setInput(fileByte);
compresser.finish();
int len = compresser.deflate(temp);
return zipBytecut(fileByte, len);
}
private byte[] zipBytecut(byte[] fileByte, int len) {
byte[] temp = new byte[len];
Deflater compresser = new Deflater();
compresser.setInput(fileByte);
compresser.finish();
compresser.deflate(temp);
return temp;
}
/**
* 解壓縮byte陣列
*
* @param fileByte
* @return
* @throws Exception
*/
public byte[] upzipByte(byte[] fileByte, int len) {
try {
Inflater decompresser = new Inflater();
decompresser.setInput(fileByte, 0, fileByte.length);
byte[] result = new byte[len];
decompresser.inflate(result);
decompresser.end();
return result;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 解壓縮檔案
*
* @param zis
* @param file
* @throws Exception
*/
private void fileUnZip(ZipInputStream zis, File file) throws Exception {
ZipEntry zip = zis.getNextEntry();
if (zip == null)
return;
String name = zip.getName();
File f = new File(file.getAbsolutePath() + "/" + name);
if (zip.isDirectory()) {
f.mkdirs();
fileUnZip(zis, file);
} else {
f.createNewFile();
FileOutputStream fos = new FileOutputStream(f);
byte b[] = new byte[2048];
int aa = 0;
while ((aa = zis.read(b)) != -1) {
fos.write(b, 0, aa);
}
fos.close();
fileUnZip(zis, file);
}
}
/**
* 解壓縮檔案zipFile儲存在directory目錄下
*
* @param directory
* @param zipFile
*/
private void unZip(String directory, String zipFile) {
try {
ZipInputStream zis = new ZipInputStream(
new FileInputStream(zipFile));
File f = new File(directory);
f.mkdirs();
fileUnZip(zis, f);
zis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 生成金鑰對並儲存到檔案
*
* @param keyfile
*/
public boolean createKey(String priKeyPath, String pubKeyPath) {
boolean b = false;
SecureRandom sr = new SecureRandom();
KeyPairGenerator kg;
try {
kg = KeyPairGenerator.getInstance("RSA");
kg.initialize(512, sr);
KeyPair kp = kg.generateKeyPair();
this.privateKey = kp.getPrivate();
this.publicKey = kp.getPublic();
File f = new File(priKeyPath); // 儲存私鑰
String dir = priKeyPath.replace(f.getName(), "");
File dirf = new File(dir);
if (!dirf.exists()) {
dirf.mkdirs();
}
f.createNewFile();
FileOutputStream fos = new FileOutputStream(f);
ObjectOutputStream dos = new ObjectOutputStream(fos);
dos.writeObject(this.privateKey);
dos.close();
fos.close();
File f2 = new File(pubKeyPath); // 儲存公鑰
String dir2 = pubKeyPath.replace(f2.getName(), "");
File dirf2 = new File(dir2);
if (!dirf2.exists()) {
dirf2.mkdirs();
}
f2.createNewFile();
FileOutputStream fos2 = new FileOutputStream(f2);
ObjectOutputStream dos2 = new ObjectOutputStream(fos2);
dos2.writeObject(this.publicKey);
dos2.close();
fos2.close();
b = true;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return b;
}
/**
* 根據key的路徑檔案獲得持久化成檔案的key
* <P>
* 例子: ZipEncrypt.getKey("E:/keypath/private.key");
*
* @param keyPath
* @return key
*/
private Key getKey(String keyPath) throws Exception {
Key key = null;
FileInputStream fis = new FileInputStream(keyPath);
ObjectInputStream ofs = new ObjectInputStream(fis);
key = (Key) ofs.readObject();
ofs.close();
fis.close();
return key;
}
/**
* 把檔案srcFile加密後儲存為destFile
*
* @param srcFile
* @param destFile
* @param privateKey
* @throws Exception
*/
private void encrypt(String srcFile, String destFile, Key privateKey)
throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);
byte[] b = new byte[53];
while (fis.read(b) != -1) {
fos.write(cipher.doFinal(b));
}
fos.close();
fis.close();
}
/**
* 把檔案srcFile解密後儲存為destFile
*
* @param srcFile
* @param destFile
* @param privateKey
* @throws Exception
*/
private void decrypt(String srcFile, String destFile, Key privateKey)
throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);
byte[] b = new byte[64];
while (fis.read(b) != -1) {
fos.write(cipher.doFinal(b));
}
fos.close();
fis.close();
}
/**
* 對byte陣列進行加密
*
* @param input
* @param keyPath
* @return
* @throws Exception
*/
public byte[] encryptByte(byte[] input, String pubKeyPath) {
try {
byte[] result = null;
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, (PublicKey) getKey(pubKeyPath));
ByteArrayInputStream in = new ByteArrayInputStream(input);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] b = new byte[53];
while (in.read(b) != -1) {
baos.write(cipher.doFinal(b));
}
result = baos.toByteArray();
baos.close();
in.close();
return result;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 對byte陣列進行解密
*
* @param input
* @param pubKeyPath
* @return
* @throws Exception
*/
public byte[] decryptByte(byte[] input, String priKeyPath) {
try {
byte[] result = null;
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, (PrivateKey) getKey(priKeyPath));
ByteArrayInputStream in = new ByteArrayInputStream(input);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] b = new byte[64];
while (in.read(b) != -1) {
baos.write(cipher.doFinal(b));
}
result = baos.toByteArray();
baos.close();
in.close();
return result;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 對目錄srcFile下的所有檔案目錄進行先壓縮後加密,然後儲存為destfile
*
* @param srcFile
* 要操作的目錄,如: "E:/srcFile"
* @param destfile
* 壓縮加密後存放的檔名,如: "E:/destfile.zip"
* @param keyfile
* 公鑰存放地點
*/
public void encryptZip(String srcFile, String destfile, String keyfile)
throws Exception {
File temp = new File(UUID.randomUUID().toString() + ".zip");
temp.deleteOnExit();
zip(srcFile, temp.getAbsolutePath());// 先壓縮檔案
encrypt(temp.getAbsolutePath(), destfile, publicKey);// 對檔案加密
temp.delete();
}
/**
* 對檔案srcfile進行先解密,然後解壓縮到目錄destfile下
*
* @param srcfile
* 要解密和解壓縮的檔名,如: "E:/srcfile.zip"
* @param destfile
* 解壓縮後的目錄,如: "E:/destfile"
* @param publicKey
* 公鑰
*/
public void decryptUnzip(String srcfile, String destfile, Key publicKey)
throws Exception {
File temp = new File(UUID.randomUUID().toString() + ".zip");
temp.deleteOnExit();
decrypt(srcfile, temp.getAbsolutePath(), publicKey); // 先對檔案解密
unZip(destfile, temp.getAbsolutePath());// 解壓縮
temp.delete();
}
public static void main(String[] args) {
ZipEncrypt encrypt=new ZipEncrypt();
encrypt.zipFile("E://zip.txt","E://zip");
}
}
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.util.UUID;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import javax.crypto.Cipher;
import com.test.common.util.file.FileUtils;
/**
* 類描述:提供加密(RSA)壓縮功能服務
*
*
* @version v1.0.0 建立時間 2009-4-4 上午11:14:56
*/
public class ZipEncrypt {
private PrivateKey privateKey = null;
private PublicKey publicKey = null;
/**
* 解壓縮檔案.
*
* @param intputFilePath
* 要解壓縮檔案路徑及檔名
* @param outputFilePath
* 解壓縮檔案輸出路徑
* @return 0:成功 1:失敗+失敗原因 (例如:1:要解壓縮的檔案不存在)
*/
public String unzipFile(String intputFilePath, String outputFilePath) {
String result = "0";
boolean b = isExists(intputFilePath);
if (!b) {
return "1:要解壓縮的檔案不存在";
} else {
File file = new File(outputFilePath);
if (!file.exists()) {
file.mkdirs();
}
File outFile = new File(outputFilePath);
if (!outFile.exists()) {
outFile.mkdirs();
}
unZip(outputFilePath, intputFilePath);
}
return result;
}
/**
* 解壓縮檔案物件.
*
* @param intputFile
* 要解壓縮的檔案物件
* @param outputFilePath
* 解壓縮檔案輸出路徑
* @return 0:成功 1:失敗+失敗原因 (例如:1:要解壓縮的檔案格式錯誤)
*/
public String unzipFile(File intputFile, String outputFilePath) {
String result = "0";
if (intputFile == null) {
return "1:要解壓縮的檔案不存在";
}
String inFileName = intputFile.getName();
String endName = inFileName.split("\\.")[1];
if (!endName.equals("zip")) {
result = "1:解壓縮的檔案格式錯誤";
} else {
File file = new File(outputFilePath);
if (!file.exists()) {
file.mkdirs();
}
String infile = intputFile.getAbsolutePath();
unZip(outputFilePath, infile);
result = "0";
}
return result;
}
/**
* 壓縮檔案.
*
* @param intputFilePath
* 要壓縮檔案路徑及檔名
* @param outputFilePath
* 壓縮檔案輸出路徑(壓縮檔案的命名為:原檔名+.zip字尾)
* @return 0:成功 1:失敗+失敗原因 (例如:1:要壓縮的檔案不存在)
*/
public String zipFile(String intputFilePath, String outputFilePath) {
String result = "0";
boolean b = isExists(intputFilePath);
if (b) {
File file = new File(intputFilePath);
result = zipFile(file, outputFilePath);
} else {
result = "1:要壓縮的檔案不存在";
}
return result;
}
public String zipFile(String intputFilePath, String outputFilePath,
String name) {
String result = "0";
boolean b = isExists(intputFilePath);
if (b) {
File file = new File(intputFilePath);
result = zipFile(file, outputFilePath, name);
} else {
result = "1:要壓縮的檔案不存在";
}
return result;
}
/**
* 壓縮檔案物件.
*
* @param intputFile
* 要壓縮的檔案物件
* @param outputFilePath
* 壓縮檔案輸出路徑(壓縮檔案的命名為:原檔名+.zip字尾)
* @return 0:成功 1:失敗+失敗原因 (例如:1:要壓縮的檔案不存在)
*/
public String zipFile(File intputFile, String outputFilePath) {
String result = "0";
if (intputFile == null) {
result = "1:要壓縮的檔案不存在";
} else {
File outfile = new File(outputFilePath);
if (!outfile.exists()) {
outfile.mkdirs();
}
String fileName = intputFile.getName();
String tempFile = "/" + FileUtils.getFileNameWithoutType(fileName)
+ ".zip";
outputFilePath += tempFile;
String indirectory = intputFile.getAbsolutePath();
zip(indirectory, outputFilePath);
return result;
}
return result;
}
@SuppressWarnings("unused")
public String zipFile(File intputFile, String outputFilePath, String name) {
String result = "0";
if (intputFile == null) {
result = "1:要壓縮的檔案不存在";
} else {
File outfile = new File(outputFilePath);
if (!outfile.exists()) {
outfile.mkdirs();
}
String fileName = intputFile.getName();
String tempFile = name + ".zip";
outputFilePath += tempFile;
String indirectory = intputFile.getAbsolutePath();
zip(indirectory, outputFilePath);
return result;
}
return result;
}
/**
* 解密檔案.
*
* @param intputFilePath
* 要解密檔案路徑及檔名
* @param outputFilePath
* 解密檔案輸出路徑
* @param prikeyPath
* 私鑰檔案路徑
* @return 0:成功 1:失敗+失敗原因 (例如:1:要解密的檔案不存在)
*/
public String decryptFile(String intputFilePath, String outputFilePath,
String prikeyPath) {
String result = "0";
boolean b = isExists(intputFilePath);
if (!b) {
return "1:要解密的檔案不存在";
}
File file = new File(outputFilePath);
if (!file.exists()) {
file.mkdirs();
}
File temp = new File(intputFilePath);
outputFilePath += "/" + temp.getName();
try {
decrypt(intputFilePath, outputFilePath, getKey(prikeyPath));
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 解密檔案物件.
*
* @param intputFile
* 解密的檔案物件
* @param outputFilePath
* 解密檔案輸出路徑
* @param prikeyPath
* 私鑰檔案路徑
* @return 0:成功 1:失敗+失敗原因 (例如:1:要解密的檔案不存在)
*/
public String decryptFile(File intputFile, String outputFilePath,
String prikeyPath) {
String result = "0";
if (intputFile == null) {
return "1:要解密的檔案不存在";
}
File file = new File(outputFilePath);
if (!file.exists()) {
file.mkdirs();
}
String inFile = intputFile.getAbsolutePath();
String inName = intputFile.getName();
outputFilePath += "/" + inName;
try {
decrypt(inFile, outputFilePath, getKey(prikeyPath));
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 加密檔案.
*
* @param intputFilePath
* 要加密檔案路徑及檔名
* @param outputFilePath
* 加密檔案輸出路徑(加密檔案的命名為:原檔名)
* @param pubkeyPath
* 公鑰檔案路徑
* @return 0:成功 1:失敗+失敗原因 (例如:1:要加密的檔案不存在)
*/
public String encryptFile(String intputFilePath, String outputFilePath,
String pubkeyPath) {
String result = "0";
boolean b = isExists(intputFilePath);
if (!b) {
return "1:要加密的檔案不存在";
} else {
File file = new File(outputFilePath);
if (!file.exists()) {
file.mkdirs();
}
File tempfile = new File(intputFilePath);
outputFilePath += "/" + tempfile.getName();
try {
encrypt(intputFilePath, outputFilePath, getKey
(pubkeyPath));
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
/**
* 加密檔案物件.
*
* @param intputFile
* 要加密的檔案物件
* @param outputFilePath
* 加密檔案輸出路徑(加密檔案的命名為:原檔名)
* @param pubkeyPath
* 公鑰檔案路徑
* @return 0:成功 1:失敗+失敗原因 (例如:1:要加密的檔案不存在)
*/
public String encryptFile(File intputFile, String outputFilePath,
String pubkeyPath) {
String result = "0";
if (intputFile == null) {
return "1:要加密的檔案不存在";
} else {
File file = new File(outputFilePath);
if (!file.exists()) {
file.mkdirs();
}
String inpath = intputFile.getAbsolutePath();
outputFilePath += "/" + intputFile.getName();
try {
encrypt(inpath, outputFilePath, getKey(pubkeyPath));
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
/**
* 判斷檔案是否存在
*
* @param inputFilePath
* @return
*/
private boolean isExists(String inputFilePath) {
boolean b = false;
File tempfile = new File(inputFilePath);
if (tempfile.exists()) {
b = true;
}
return b;
}
/**
* 目錄壓縮
*
* @param out
* @param f
* @param base
* @throws Exception
*/
private void directoryZip(ZipOutputStream out, File f, String base)
throws Exception {
if (f.isDirectory()) {
File[] fl = f.listFiles();
out.putNextEntry(new ZipEntry(base + "/"));
if (base.length() == 0) {
base = "";
} else {
base = base + "/";
}
for (int i = 0; i < fl.length; i++) {
directoryZip(out, fl[i], base + fl[i].getName());
}
} else {
out.putNextEntry(new ZipEntry(base));
FileInputStream in = new FileInputStream(f);
byte[] bb = new byte[2048];
int aa = 0;
while ((aa = in.read(bb)) != -1) {
out.write(bb, 0, aa);
}
in.close();
}
}
/**
* 壓縮檔案
*
* @param zos
* @param file
* @throws Exception
*/
private void fileZip(ZipOutputStream zos, File file) throws Exception {
if (file.isFile()) {
zos.putNextEntry(new ZipEntry(file.getName()));
FileInputStream fis = new FileInputStream(file);
byte[] bb = new byte[2048];
int aa = 0;
while ((aa = fis.read(bb)) != -1) {
zos.write(bb, 0, aa);
}
fis.close();
} else {
directoryZip(zos, file, "");
}
}
/**
* 對directory目錄下的檔案壓縮,儲存為指定的檔案zipFile
*
* @param directory
* @param zipFile
*/
private void zip(String directory, String zipFile) {
try {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(
zipFile));
fileZip(zos, new File(directory));
zos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 壓縮byte陣列
*
* @param fileByte
* @return
*/
public byte[] zipByte(byte[] fileByte) {
byte[] temp = new byte[fileByte.length];
Deflater compresser = new Deflater();
compresser.setInput(fileByte);
compresser.finish();
int len = compresser.deflate(temp);
return zipBytecut(fileByte, len);
}
private byte[] zipBytecut(byte[] fileByte, int len) {
byte[] temp = new byte[len];
Deflater compresser = new Deflater();
compresser.setInput(fileByte);
compresser.finish();
compresser.deflate(temp);
return temp;
}
/**
* 解壓縮byte陣列
*
* @param fileByte
* @return
* @throws Exception
*/
public byte[] upzipByte(byte[] fileByte, int len) {
try {
Inflater decompresser = new Inflater();
decompresser.setInput(fileByte, 0, fileByte.length);
byte[] result = new byte[len];
decompresser.inflate(result);
decompresser.end();
return result;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 解壓縮檔案
*
* @param zis
* @param file
* @throws Exception
*/
private void fileUnZip(ZipInputStream zis, File file) throws Exception {
ZipEntry zip = zis.getNextEntry();
if (zip == null)
return;
String name = zip.getName();
File f = new File(file.getAbsolutePath() + "/" + name);
if (zip.isDirectory()) {
f.mkdirs();
fileUnZip(zis, file);
} else {
f.createNewFile();
FileOutputStream fos = new FileOutputStream(f);
byte b[] = new byte[2048];
int aa = 0;
while ((aa = zis.read(b)) != -1) {
fos.write(b, 0, aa);
}
fos.close();
fileUnZip(zis, file);
}
}
/**
* 解壓縮檔案zipFile儲存在directory目錄下
*
* @param directory
* @param zipFile
*/
private void unZip(String directory, String zipFile) {
try {
ZipInputStream zis = new ZipInputStream(
new FileInputStream(zipFile));
File f = new File(directory);
f.mkdirs();
fileUnZip(zis, f);
zis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 生成金鑰對並儲存到檔案
*
* @param keyfile
*/
public boolean createKey(String priKeyPath, String pubKeyPath) {
boolean b = false;
SecureRandom sr = new SecureRandom();
KeyPairGenerator kg;
try {
kg = KeyPairGenerator.getInstance("RSA");
kg.initialize(512, sr);
KeyPair kp = kg.generateKeyPair();
this.privateKey = kp.getPrivate();
this.publicKey = kp.getPublic();
File f = new File(priKeyPath); // 儲存私鑰
String dir = priKeyPath.replace(f.getName(), "");
File dirf = new File(dir);
if (!dirf.exists()) {
dirf.mkdirs();
}
f.createNewFile();
FileOutputStream fos = new FileOutputStream(f);
ObjectOutputStream dos = new ObjectOutputStream(fos);
dos.writeObject(this.privateKey);
dos.close();
fos.close();
File f2 = new File(pubKeyPath); // 儲存公鑰
String dir2 = pubKeyPath.replace(f2.getName(), "");
File dirf2 = new File(dir2);
if (!dirf2.exists()) {
dirf2.mkdirs();
}
f2.createNewFile();
FileOutputStream fos2 = new FileOutputStream(f2);
ObjectOutputStream dos2 = new ObjectOutputStream(fos2);
dos2.writeObject(this.publicKey);
dos2.close();
fos2.close();
b = true;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return b;
}
/**
* 根據key的路徑檔案獲得持久化成檔案的key
* <P>
* 例子: ZipEncrypt.getKey("E:/keypath/private.key");
*
* @param keyPath
* @return key
*/
private Key getKey(String keyPath) throws Exception {
Key key = null;
FileInputStream fis = new FileInputStream(keyPath);
ObjectInputStream ofs = new ObjectInputStream(fis);
key = (Key) ofs.readObject();
ofs.close();
fis.close();
return key;
}
/**
* 把檔案srcFile加密後儲存為destFile
*
* @param srcFile
* @param destFile
* @param privateKey
* @throws Exception
*/
private void encrypt(String srcFile, String destFile, Key privateKey)
throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);
byte[] b = new byte[53];
while (fis.read(b) != -1) {
fos.write(cipher.doFinal(b));
}
fos.close();
fis.close();
}
/**
* 把檔案srcFile解密後儲存為destFile
*
* @param srcFile
* @param destFile
* @param privateKey
* @throws Exception
*/
private void decrypt(String srcFile, String destFile, Key privateKey)
throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);
byte[] b = new byte[64];
while (fis.read(b) != -1) {
fos.write(cipher.doFinal(b));
}
fos.close();
fis.close();
}
/**
* 對byte陣列進行加密
*
* @param input
* @param keyPath
* @return
* @throws Exception
*/
public byte[] encryptByte(byte[] input, String pubKeyPath) {
try {
byte[] result = null;
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, (PublicKey) getKey(pubKeyPath));
ByteArrayInputStream in = new ByteArrayInputStream(input);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] b = new byte[53];
while (in.read(b) != -1) {
baos.write(cipher.doFinal(b));
}
result = baos.toByteArray();
baos.close();
in.close();
return result;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 對byte陣列進行解密
*
* @param input
* @param pubKeyPath
* @return
* @throws Exception
*/
public byte[] decryptByte(byte[] input, String priKeyPath) {
try {
byte[] result = null;
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, (PrivateKey) getKey(priKeyPath));
ByteArrayInputStream in = new ByteArrayInputStream(input);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] b = new byte[64];
while (in.read(b) != -1) {
baos.write(cipher.doFinal(b));
}
result = baos.toByteArray();
baos.close();
in.close();
return result;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 對目錄srcFile下的所有檔案目錄進行先壓縮後加密,然後儲存為destfile
*
* @param srcFile
* 要操作的目錄,如: "E:/srcFile"
* @param destfile
* 壓縮加密後存放的檔名,如: "E:/destfile.zip"
* @param keyfile
* 公鑰存放地點
*/
public void encryptZip(String srcFile, String destfile, String keyfile)
throws Exception {
File temp = new File(UUID.randomUUID().toString() + ".zip");
temp.deleteOnExit();
zip(srcFile, temp.getAbsolutePath());// 先壓縮檔案
encrypt(temp.getAbsolutePath(), destfile, publicKey);// 對檔案加密
temp.delete();
}
/**
* 對檔案srcfile進行先解密,然後解壓縮到目錄destfile下
*
* @param srcfile
* 要解密和解壓縮的檔名,如: "E:/srcfile.zip"
* @param destfile
* 解壓縮後的目錄,如: "E:/destfile"
* @param publicKey
* 公鑰
*/
public void decryptUnzip(String srcfile, String destfile, Key publicKey)
throws Exception {
File temp = new File(UUID.randomUUID().toString() + ".zip");
temp.deleteOnExit();
decrypt(srcfile, temp.getAbsolutePath(), publicKey); // 先對檔案解密
unZip(destfile, temp.getAbsolutePath());// 解壓縮
temp.delete();
}
public static void main(String[] args) {
ZipEncrypt encrypt=new ZipEncrypt();
encrypt.zipFile("E://zip.txt","E://zip");
}
}