已有256人关注
老师,大佬求助
发表在Java答疑区 2021-10-03 悬赏:1 学分
是否精华
版块置顶:
问题描述:压缩成功而且速度提高,但出现压缩图片的问题,如果压缩的是文件夹会出现下第一张图问题
,但是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();
 }
}

 

2021-10-04 14:12:27编辑
分享到:
精彩评论 1
game99258
学分:2266 LV10
2022-06-24
沙发

老师没注意这个贴

首页上一页 1 下一页尾页 1 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照