1、 需求
我想把資源文件先加密成壓縮文件,然后同時創建每個加密壓縮文件同名的文件夾,同時需要把這個加密文件拷貝到這個同名的文件夾,然后還需要把一個圖片和一個文本文檔同時放進這個文件夾,然后在不加密壓縮這個文件夾,上傳資源到百度網盤。
2、思路
1)、windows環境用python來實現,能實現生成同名文件夾,然后拷貝文件的時候中文名的文件拷貝都文件夾有問題,估計是編碼格式不對,對python不是很熟悉,然后放棄python來處理。
2)、用java來實現,遍歷加密文件,然后創建同名(不帶后綴)的文件夾,然后把這個加密文件拷貝到這個文件夾,然后刪除加密文件,再拷貝圖片和文本到這個加密文件夾。
3、具體代碼實現過程
1)、批量加密文件
windows環境批量加密文件,我們在需要加密的文件目錄新建1.bat文件,內容如下
for %%X in (*) do "C:\Program Files\7-Zip\7z.exe" a "%%~dpnX.7z" "%%X" -pcch1233nxecc -mhe -mx=0
注意這里-p后面是密碼,這里的密碼是cch1233nxecc,用戶可以自己設置,點擊1.bat就能加密批量壓縮。
2)、把壓縮成7z的文件手動拿出來,放到一個文件夾里面去,我放在這個目錄
F:\didi1\all11_30\C++
3)、用java代碼實現創建文件夾,然后放拷貝文件進去,然后刪除加密文件,然后拷貝具體圖片和文本文檔到這個文件夾,解壓密碼圖片和文本地址如下
F:\\didi1\\all11_30\\解壓密碼.png
F:\\didi1\\all11_30\\計算機40多個方向1000多個電子書.txt
代碼實現如下
package com.chenyu;import java.io.File;public class Test {public static void main(String[] args) {System.out.println("chen"); String path = "F:\\didi1\\all11_30\\C++";File decodePwdPngFile = new File("F:\\didi1\\all11_30\\解壓密碼.png");File txtFile = new File("F:\\didi1\\all11_30\\計算機40多個方向1000多個電子書.txt");FileUtil.handleFile(path, decodePwdPngFile, txtFile);}}
FileUtil.java文件如下?
package com.chenyu;import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.channels.FileChannel;public class FileUtil {/*** 創建新的文件夾* @param file* @return*/public static String getNewDirectoryPath(File file) {if (file == null)return "chenyu";String dirPath = file.getAbsolutePath() + File.separator + file.getName();if (dirPath.contains(".")) {int index = dirPath.indexOf('.');dirPath = dirPath.substring(0, index);} else {dirPath = dirPath + "1";}return dirPath;}/*** 處理文件,創建文件夾,然后拷貝文件進去* @param path*/public static void handleFile(String path, File decodePwdPngFile, File txtFile) {if (path == null || "".equals(path) || decodePwdPngFile == null || txtFile == null)return;File file = new File(path);if (!file.exists())return;File[] files = file.listFiles();if (files == null || files.length == 0)return;try {for (File childFile : files) {if (childFile.isDirectory()) {System.out.println("文件夾路徑是:" + childFile.getAbsolutePath());handleFile(childFile.getAbsolutePath(), decodePwdPngFile, txtFile);} else {System.out.println("文件路徑是:" + childFile.getAbsolutePath());if (childFile.getAbsolutePath() == null)return;if (childFile.getName() == null)return;//得到新的目錄String dirPath = getNewDirectoryPath(childFile);System.out.println("dirPath路徑是:" + dirPath);File fileDir = new File(dirPath);//創建新的目錄if (!fileDir.exists()) {fileDir.mkdirs();}//構建資源zip文件File fileTmp = new File(dirPath, childFile.getName());if (!fileTmp.exists()) {fileTmp.createNewFile();}//復制資源zip文件到新的文件夾copyFile(childFile, fileTmp);//刪除zip文件childFile.delete();//拷貝圖片到這個dirPath目錄來File decodePwdPng = new File(dirPath, "解壓密碼.png");if (!decodePwdPng.exists()) {decodePwdPng.createNewFile();}copyFile(decodePwdPngFile, decodePwdPng);//拷貝txt文件到這個dirPath目錄來File newTxtFile = new File(dirPath, "計算機40多個方向1000多個電子書.txt");if (!newTxtFile.exists()) {newTxtFile.createNewFile();}copyFile(txtFile, newTxtFile);}}} catch (Exception e) {e.printStackTrace();}}/*** 文件復制* @param srcFile* @param dstFile* @throws IOException*/public static void copyFile(File srcFile, File dstFile) throws IOException {if (srcFile == null || !srcFile.exists()) {return;}if (dstFile == null || !dstFile.exists()) {return;}FileInputStream fileIns = null;FileOutputStream fileOuts = null;FileChannel source = null;FileChannel destination = null;try {fileIns = new FileInputStream(srcFile);fileOuts = new FileOutputStream(dstFile);source = fileIns.getChannel();destination = fileOuts.getChannel();destination.transferFrom(source, 0, source.size());} catch (Exception e) {e.printStackTrace();} finally {if (fileIns != null)fileIns.close();if (fileOuts != null)fileOuts.close();if (source != null)source.close();if (destination != null)destination.close();}}
}
? 控制臺打印
chen
文件路徑是:F:\didi1\all11_30\C++\C++ Primer Plus中文版.7z
dirPath路徑是:F:\didi1\all11_30\C++\C++ Primer Plus中文版
文件路徑是:F:\didi1\all11_30\C++\C專家編程.7z
dirPath路徑是:F:\didi1\all11_30\C++\C專家編程
文件路徑是:F:\didi1\all11_30\C++\C和指針(第二版)高清.7z
dirPath路徑是:F:\didi1\all11_30\C++\C和指針(第二版)高清
文件路徑是:F:\didi1\all11_30\C++\C語言深度解剖.7z
dirPath路徑是:F:\didi1\all11_30\C++\C語言深度解剖
文件路徑是:F:\didi1\all11_30\C++\C陷阱與缺陷.7z
dirPath路徑是:F:\didi1\all11_30\C++\C陷阱與缺陷
文件路徑是:F:\didi1\all11_30\C++\Effective C 中文版.7z
dirPath路徑是:F:\didi1\all11_30\C++\Effective C 中文版
文件路徑是:F:\didi1\all11_30\C++\Effective C++(第三版中文版).7z
dirPath路徑是:F:\didi1\all11_30\C++\Effective C++(第三版中文版)
文件路徑是:F:\didi1\all11_30\C++\Effective STL(中文版).7z
dirPath路徑是:F:\didi1\all11_30\C++\Effective STL(中文版)
文件路徑是:F:\didi1\all11_30\C++\LinuxC編程一站式學習.7z
dirPath路徑是:F:\didi1\all11_30\C++\LinuxC編程一站式學習
文件路徑是:F:\didi1\all11_30\C++\More Effective C++.7z
dirPath路徑是:F:\didi1\all11_30\C++\More Effective C++
文件路徑是:F:\didi1\all11_30\C++\STL源碼剖析(侯捷譯).7z
dirPath路徑是:F:\didi1\all11_30\C++\STL源碼剖析(侯捷譯)
文件路徑是:F:\didi1\all11_30\C++\UNIX編程藝術(中文).7z
dirPath路徑是:F:\didi1\all11_30\C++\UNIX編程藝術(中文)
文件路徑是:F:\didi1\all11_30\C++\中文版:C++Primer(第三版).7z
dirPath路徑是:F:\didi1\all11_30\C++\中文版:C++Primer(第三版)
這里就可以得到我們的結果了
?
但是我們還需要再把這個文件夾批量不加密壓縮,我們依然還是新建2.bat,內容如下
for /d %%X in (*) do "c:\Program Files\7-Zip\7z.exe" a "%%X.7z" "%%X\"
點擊2.bat文件,效果如下
這里的內容就是我們需要的內容,然后我們需要批量單個上傳,我有工具,非常方便。