package com.framework.test;
import it.sauronsoftware.base64.Base64;
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.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
public class test {
// Base64 解密成明文
public static void toJavaDecrypt(String sourceFile, String armFile)
throws IOException {
InputStream inputStream = new FileInputStream(sourceFile);
OutputStream outputStream = new FileOutputStream(armFile);
Base64.decode(inputStream, outputStream);
System.out.println("解密成功,保存的文件名为" + armFile);
outputStream.close();
inputStream.close();
}
// Base64 加密成Java密文
public static void toJavaEncrypt(String sourceFile, String armFile)
throws IOException {
InputStream inputStream = new FileInputStream(sourceFile);
OutputStream outputStream = new FileOutputStream(armFile);
Base64.encode(inputStream, outputStream);
System.out.println("加密成功,保存的文件名为" + armFile);
outputStream.close();
inputStream.close();
}
/**
* 压缩一层目录下的所有文件或压缩单个文件
*
* @param filePath
* 要压缩的文件路径 (单个文件需含文件名)
* @param toFilePath
* 压缩后的文件路径 含文件名
* @param isDelete
* 是否删除原路径 true为删除 false为不删除
*/
public static void zip(String filePath, String toFilePath, boolean isDelete) {
File file = new File(filePath);
if (file.isDirectory()) {// 是否为文件目录,是为true
zipFiles(filePath, toFilePath);
if (isDelete) {
String[] files = file.list();
for (int i = 0; i < files.length; i++) {
File f = new File(filePath + "\\" + files[i]);
f.delete();
}
file.delete();
}
} else {
zipFile(filePath, toFilePath);
if (isDelete) {
file.delete();
}
}
System.out.println("打包成功:"+toFilePath);
}
/**
* 压缩某路径的单个文件
*
* @param filePath
* 要压缩的文件的路径(含文件名 如:d:\\tt.txt)
* @param zipname
* 压缩后的路径(含压缩后文件名 如:d:\\tt.zip)
*/
private static void zipFile(String filePath, String zipname) {
FileOutputStream fos = null;
ZipOutputStream zipOut = null;
try {
fos = new FileOutputStream(zipname);// 创建文件输出流(低级流)
zipOut = new ZipOutputStream(fos);// 创建zip文件输出流
File f = new File(filePath);
FileInputStream fis = new FileInputStream(filePath);
ZipEntry ze = new ZipEntry(f.getName());
zipOut.putNextEntry(ze);
int c = 0;
while ((c = fis.read()) != -1) {
zipOut.write(c);
}
fis.close();
zipOut.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* 压缩某路径下所有文件
*
* @param filePath
* 要压缩的路径
* @param zipname
* 压缩后的路径(含压缩后文件名 如:d:\\tt.zip)
*/
private static void zipFiles(String filePath, String zipname) {
FileOutputStream fos = null;
ZipOutputStream zipOut = null;
String[] files = (new File(filePath)).list();
try {
fos = new FileOutputStream(zipname);// 创建文件输出流(低级流)
zipOut = new ZipOutputStream(fos);// 创建zip文件输出流
int i = 0;
for (i = 0; i < files.length; i++) {
File f = new File(files[i]);
FileInputStream fis = new FileInputStream(filePath + "\\"
+ files[i]);
ZipEntry ze = new ZipEntry(f.getName());
zipOut.putNextEntry(ze);
int c = 0;
while ((c = fis.read()) != -1) {
zipOut.write(c);
}
fis.close();
}
zipOut.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static void unZipFile(String destfile, String surfile) {
int BUFFER = 2048;
try {
String filePath = destfile + "\\";
System.out.println("surfile:"+surfile);
ZipFile zipFile = new ZipFile(surfile);
Enumeration emu = zipFile.entries();
while (emu.hasMoreElements()) {
ZipEntry entry = (ZipEntry) emu.nextElement();
// 会把目录作为一个file读出一次,所以只建立目录就可以,之下的文件还会被迭代到。
if (entry.isDirectory()) {
new File(filePath, entry.getName()).mkdirs();
continue;
}
BufferedInputStream bis = new BufferedInputStream(zipFile
.getInputStream(entry));
File file = new File(filePath, entry.getName());
// 加入这个的原因是zipfile读取文件是随机读取的,这就造成可能先读取一个文件
// 而这个文件所在的目录还没有出现过,所以要建出目录来。
File parent = file.getParentFile();
if (parent != null && (!parent.exists())) {
parent.mkdirs();
}
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);
int count;
byte data[] = new byte[BUFFER];
while ((count = bis.read(data, 0, BUFFER)) != -1) {
bos.write(data, 0, count);
}
bos.flush();
bos.close();
bis.close();
}
zipFile.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
// toJavaDecrypt("d:\\Base64加密后(c#).xml","d:\\decoded.xml");
// toJavaEncrypt("d:\\3500006405明码.xml","d:\\3500006405加密.xml");
// toJavaDecrypt("d:\\123.xml","d:\\12345.xml");
// zip("d:\\ww.sql", "d:\\123.zip", false); //压缩
//unZipFile("d:\\123", "d:\\学习记录(无密码).zip"); // 解压缩
Base64Helper b = new Base64Helper();
InputStream inputStream = new FileInputStream("d:\\123\\StudyLog.xml");
OutputStream outputStream = new FileOutputStream("d:\\123\\12345.xml");
b.decodeBuffer(inputStream, outputStream);
}
}
base64转码解密成明文加密成Java密文
4星 · 超过85%的资源 需积分: 50 182 浏览量
2009-08-04
14:53:50
上传
评论 3
收藏 4KB RAR 举报
marc0658
- 粉丝: 0
- 资源: 1
最新资源
- 第十四章 scikit-learn 库(jupyter版 人工智能编程基础)
- 天天视频(一款功能强大的影视剧应用 电.apk
- 一个xxs做的python小游戏,当对方血量比你低时可以一直攻击(什么时候改改?)
- 第十三章 Matplotlib库(jupyter版 人工智能编程基础)
- 基于yolov8的人体动作识别检测项目源码.zip
- 自然语言处理大作业-python的汉语词义自动消歧系统源码+报告.zip
- MT管理器_2.15.3-会员功能解锁.apk
- 基于机器学习的共享单车使用量分析与预测
- 基于Python开发的汉语词义自动消歧系统源码+超详细注释+实验报告(95分以上).zip
- 51单片机汇编语言音乐盒+电子琴,proteus仿真运行正常,开发板实物运行正常 代码都带有注释,方便理解和学习
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
前往页