生成文件
public class FileSM4AndZip {
private static final String CSV_COLUMN_SEPARATOR = "\u001B";
private static final String CSV_ROW_SEPARATOR = "\n";
public static void fileEncAndZip(String fileName, String[] titleColumn, List<Map<String, Object>> dataList,String filePath, String year, String month){
String name = fileName + ".dat";
filePath = createFile(name, titleColumn, dataList,filePath, year, month);
String encFileName =filePath + fileName + ".sm4";
String zipFileName =filePath + fileName + ".zip";
SM4Tools.encryptFile( filePath + name, encFileName);
FileZip.compression( zipFileName,new File( encFileName));
File file = new File(filePath + name);
file.delete();
File file1 = new File(encFileName);
file1.delete();
}
private static String createFile( String fileName, String[] titleColumn, List<Map<String, Object>> dataList,String filePath, String year, String month) {
OutputStream out = null;
try {
filePath = filePath + year + "\\" + month + "\\";
String path = filePath + fileName;
File file = new File(path);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
if (!file.exists()) {
file.createNewFile();
}
out = new FileOutputStream(path);
StringBuilder buf = new StringBuilder();
for (Map<String, Object> map : dataList) {
for (int i = 0; i < titleColumn.length; i++) {
String title = titleColumn[i].trim();
if (!"".equals(title)) {
buf.append(map.get(title));
if (i != titleColumn.length - 1) {
buf.append(CSV_COLUMN_SEPARATOR);
}
}
}
buf.append(CSV_ROW_SEPARATOR);
}
out.write(buf.toString().getBytes(StandardCharsets.UTF_8));
return filePath;
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (out != null) {
try {
out.flush();
out.close();
} catch (IOException ignored) {
}
}
}
}
}
sm4 加密
public class SM4Tools {
private static final String key = "C7046F00DWID8583F1AB0AJFNA5E470E";
private static final byte[] keyData = ByteUtils.fromHexString(key);
static{
if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null){
Security.addProvider(new BouncyCastleProvider());
}
}
public static Cipher generateCipher(int mode,byte[] keyData) throws InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException, NoSuchProviderException {
Cipher cipher = Cipher.getInstance("SM4/ECB/PKCS5Padding", BouncyCastleProvider.PROVIDER_NAME);
Key sm4Key = new SecretKeySpec(keyData, "SM4");
cipher.init(mode, sm4Key);
return cipher;
}
public static void encryptFile(String sourcePath, String targetPath){
try {
Cipher cipher = generateCipher(Cipher.ENCRYPT_MODE, SM4Tools.keyData);
CipherInputStream cipherInputStream = new CipherInputStream(new FileInputStream(sourcePath), cipher);
FileUtil.writeFromStream(cipherInputStream, targetPath);
IoUtil.close(cipherInputStream);
} catch (InvalidKeyException | NoSuchPaddingException | NoSuchAlgorithmException | NoSuchProviderException |
FileNotFoundException e) {
e.printStackTrace();
}
}
public static void decryptFile(String sourcePath, String targetPath) {
System.out.println("==================开始解密========================");
FileInputStream in =null;
ByteArrayInputStream byteArrayInputStream =null;
OutputStream out = null;
CipherOutputStream cipherOutputStream=null;
try {
in = new FileInputStream(sourcePath);
byte[] bytes = IoUtil.readBytes(in);
byteArrayInputStream = IoUtil.toStream(bytes);
Cipher cipher = generateCipher(Cipher.DECRYPT_MODE, SM4Tools.keyData);
out = Files.newOutputStream(Paths.get(targetPath));
cipherOutputStream = new CipherOutputStream(out, cipher);
IoUtil.copy(byteArrayInputStream, cipherOutputStream);
} catch (IOException | NoSuchPaddingException | InvalidKeyException | NoSuchAlgorithmException |
NoSuchProviderException e) {
e.printStackTrace();
} finally {
IoUtil.close(cipherOutputStream);
IoUtil.close(out);
IoUtil.close(byteArrayInputStream);
IoUtil.close(in);
}
System.out.println("==================解密完成========================");
}
文件压缩
public class FileZip {
public static void compression(String zipFileName,File targetFile) {
System.out.println("正在压缩...");
try {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
BufferedOutputStream bos = new BufferedOutputStream(out);
zip(out,targetFile,targetFile.getName(),bos);
bos.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("压缩完成!");
}
private static void zip(ZipOutputStream zOut, File targetFile,
String name, BufferedOutputStream bos) throws IOException {
if(targetFile.isDirectory()) {
File[] files = targetFile.listFiles();
if(targetFile.length() == 0) {
zOut.putNextEntry(new ZipEntry(name + "/"));
}
for(File f: files){
zip(zOut,f,name + "/" + f.getName(),bos);
}
}else {
zOut.putNextEntry(new ZipEntry(name));
InputStream in = new FileInputStream(targetFile);
BufferedInputStream bis = new BufferedInputStream(in);
byte[] bytes = new byte[1024];
int len = -1;
while((len = bis.read(bytes)) != -1) {
bos.write(bytes,0,len);
}
bis.close();
}
}
public static Boolean decompression(String targetFileName,String parent) {
System.out.println("正在解压...");
try {
ZipInputStream zIn = new ZipInputStream(Files.newInputStream(Paths.get(targetFileName)));
ZipEntry entry = null;
File file = null;
while((entry = zIn.getNextEntry()) !=null && !entry.isDirectory()){
file = new File(parent,entry.getName());
if(!file.exists()) {
new File(file.getParent()).mkdirs();
}
OutputStream out = Files.newOutputStream(file.toPath());
BufferedOutputStream bos = new BufferedOutputStream(out);
byte[] bytes = new byte[1024];
int len = -1;
while((len = zIn.read(bytes)) != -1) {
bos.write(bytes,0,len);
}
bos.close();
System.out.println(file.getAbsolutePath() + "解压成功!");
}
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}