带注释解释代码
package com.zcl.Test;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.Socket;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class Test1 {
public static void main(String[] args) {
String url = "https://gitcode.net/tasking/Encrypt-decrypt-files/-/raw/master/AES.zip";
DownAndReadFile(url);
}
public static void DownAndReadFile(String filePath) {
long startTime = System.currentTimeMillis();
String data = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
String dir = "F:\\2022C4java基础认证2\\远程下载解压解密压缩任务\\code\\远程请求下载" + data;
File saverPath = new File(dir);
if (!saverPath.exists()) {
saverPath.mkdir();
}
String[] urlName = filePath.split("/");
int len = urlName.length - 1;
String uname = urlName[len];
try {
File file = new File(saverPath + "//" + uname);
if (file != null && !file.exists()) {
file.createNewFile();
}
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
URL url = new URL(filePath);
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
uc.setDoInput(true);
uc.connect();
InputStream inputStream = uc.getInputStream();
System.out.println("file size is:" + uc.getContentLength());
byte[] b = new byte[1024 * 4];
int byteRead = -1;
while ((byteRead = inputStream.read(b)) != -1) {
bufferedOutputStream.write(b, 0, byteRead);
}
inputStream.close();
bufferedOutputStream.close();
long endTime = System.currentTimeMillis();
System.out.println("下载耗时:" + (endTime - startTime) / 1000 * 1.0 + "s");
System.out.println("文件下载成功!");
StringBuffer strb = new StringBuffer();
BufferedInputStream fs = new BufferedInputStream(new FileInputStream(saverPath + "//" + uname));
BufferedReader br = new BufferedReader(new InputStreamReader(fs, "UTF-8"));
String date = "";
while ((date = br.readLine()) != null) {
strb.append(data + "\n");
}
br.close();
fs.close();
System.out.println("解压文件中...");
unZipFiles(dir + "/AES.zip", dir + "/");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void unZipFiles(String zipPath, String descDir) throws Exception {
System.out.println("解压文件的名称:" + zipPath + "\n解压的文件存放路径:" + descDir);
unZipFiles(new File(zipPath), descDir);
}
@SuppressWarnings("rawtypes")
public static void unZipFiles(File zipFile, String descDir) throws Exception {
File pathFile = new File(descDir);
if (!pathFile.exists()) {
pathFile.mkdirs();
}
ZipFile zip = new ZipFile(zipFile);
for (Enumeration entries = zip.entries(); entries.hasMoreElements(); ) {
ZipEntry entry = (ZipEntry) entries.nextElement();
String zipEntryMame = entry.getName();
InputStream in = zip.getInputStream(entry);
String outPath = (descDir + zipEntryMame).replaceAll("\\*", "//");
File file = new File(outPath.substring(0, outPath.lastIndexOf("/")));
if (!file.exists()) {
file.mkdirs();
}
if (new File(outPath).isDirectory()) {
continue;
}
System.out.println(outPath);
FileOutputStream out = new FileOutputStream(outPath);
byte[] byf1 = new byte[1024];
int len;
while ((len = in.read(byf1)) != -1) {
out.write(byf1);
}
in.close();
out.close();
}
System.out.println("文件解压成功");
}
}
|