问题描述:压缩成功而且速度提高,但出现压缩图片的问题,如果压缩的是文件夹会出现下第一张图问题 ,但是zip文件仍可以解压打开并可以正常使用, 如果压缩为但个文件则出现第三张图的情况,十分疑惑。 希望老师能不吝赐教,因为代码有些多还请老师复制去环境中看看,谢谢
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipMAXProject {
static long sx, js;
static String yuan = null, ya = null, bag = "";
static void Sm(File[] xxx, ZipOutputStream xx, String bag) throws IOException {
for (int i = 0; xxx != null && i < xxx.length; i++) {
if (xxx[i] != null && xxx[i].isFile()) {
int a = -1;
byte ls[] = new byte[1024];
xx.putNextEntry(new ZipEntry(bag + xxx[i].getName()));
BufferedInputStream dis = new BufferedInputStream(new FileInputStream(xxx[i]));
while ((a = dis.read(ls)) != -1) {
xx.write(ls, 0, a);
xx.flush();
}
dis.close();
xx.closeEntry();
} else if (xxx[i] != null && xxx[i].isDirectory()) {
Sm(xxx[i].listFiles(), xx, bag + xxx[i].getName() + File.separator);
}
}
}
static void pad(File file) throws FileNotFoundException, IOException {
if (file.isDirectory()) {
if (file.listFiles().length == 1) {
pad(file.listFiles()[0]);
} else {
File a[] = new File[(int) (file.listFiles().length / 2 + 1)];
File b[] = new File[(int) (file.listFiles().length / 2 + 1)];
System.arraycopy(file.listFiles(), 0, a, 0, (int) (file.listFiles().length / 2) - 1);
System.arraycopy(file.listFiles(), (int) (file.listFiles().length / 2), b, 0,
file.listFiles().length - (int) (file.listFiles().length / 2));
Thread threadOne = new Thread(new Runnable() {
public void run() {
try {
sx = System.currentTimeMillis();
Sm(a, new ZipOutputStream(new FileOutputStream(ya)), bag);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
Thread threadTwo = new Thread(new Runnable() {
public void run() {
try {
Sm(b, new ZipOutputStream(new FileOutputStream(ya)), bag);
System.out.println("**************压缩用时 :" + (System.currentTimeMillis()- sx) + " ms");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
threadOne.start();
threadTwo.start();
}
} else if (file.isFile()) {
Sm(file.listFiles(), new ZipOutputStream(new FileOutputStream(ya)), bag);
System.out.println("压缩成功*******************");
}
}
public static void main(String[] args) {
Scanner kzt = new Scanner(System.in);
String[] ou = { "-------------多引擎测试开发", "请输入源文件路径", "请输入压缩存放路径" };
for (int i = 0; i < 3; i++) {
switch (i) {
case 0:
System.out.println(ou[0]);
break;
case 1:
System.out.println(ou[1]);
yuan = kzt.next();
break;
case 2:
System.out.println(ou[2]);
ya = kzt.next();
break;
}
}
if (new File(yuan).exists()) { // 判断文件是否存在
try {
System.out.println("开始压缩");
pad(new File(yuan));
} catch (IOException e) {
e.printStackTrace();
}
}
kzt.close();
}
}