package com.demo.zip; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; /** * * @Description 测试zip压缩 * @author HackerD * @version 1.0 * @CreateAt 2013-5-22 下午4:11:09 */ public class TestZipFile { private int[] no = { 1, 3, 4, 5, 7, 9, 12 }; private double[] scores = { 77.4, 67.5, 88.3, 78.8, 98.5, 78.4, 76.4 }; /** * * @Description 将人数学号成绩按照格式:人数-学号1;学号2...-成绩1;成绩2... 写入文本文件 * @author HackerD * @CreateAt 2013-5-22 下午7:40:49 * @version V1.0 * @ModificationHistory<BR> * Date Author Version Description <BR> * ---------------------------------------------- * */ public void writeData() { FileWriter fw = null; BufferedWriter bfw = null; try { fw = new FileWriter("Score.txt"); bfw = new BufferedWriter(fw); String noStr = ""; String scoreStr = ""; // 拼凑学号跟成绩 for (int i = 0; i < no.length; i++) { noStr += no[i] + ";"; scoreStr += scores[i] + ";"; } // 写入文件 bfw.write(no.length + "-" + noStr.substring(0, noStr.length() - 1) + "-" + scoreStr.substring(0, scoreStr.length() - 1)); } catch (Exception e) { e.printStackTrace(); } finally { try { bfw.close(); fw.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * * @Description 根据写入的格式从Score.txt中找出最高成绩和对应学号 * @author HackerD * @CreateAt 2013-5-22 下午7:41:01 * @version V1.0 * @ModificationHistory<BR> * Date Author Version Description <BR> * ---------------------------------------------- * */ public void findMaxScore() { FileReader fr = null; BufferedReader bfr = null; String info = ""; String temp = ""; double maxScore = 0; int pos = 0; try { fr = new FileReader("Score.txt"); bfr = new BufferedReader(fr); // 读取信息存放在info中 while ((temp = bfr.readLine()) != null) { info += temp; } // 拆分信息 String[] infoarr = info.split("-"); // 拆分出成绩 String[] scores = infoarr[2].split(";"); // 拆分出学号 String[] no = infoarr[1].split(";"); // 找出最高成绩 for (int i = 0; i < scores.length; i++) { if (Double.parseDouble(scores[i]) > maxScore) { maxScore = Double.parseDouble(scores[i]); pos = i; } } // 打印结果 System.out.println("最高成绩为:" + maxScore + ";学号为:" + no[pos]); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { bfr.close(); fr.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * * @Description 将Score.txt压缩Score.zip * @author HackerD * @CreateAt 2013-5-22 下午7:41:13 * @version V1.0 * @ModificationHistory<BR> * Date Author Version Description <BR> * ---------------------------------------------- * */ public void scoreZip() { try { InputStream is = new FileInputStream("Score.txt"); OutputStream os = new FileOutputStream("Score.zip"); ZipOutputStream zf = new ZipOutputStream(os); // 添加条目 zf.putNextEntry(new ZipEntry("Score.txt")); int n = 0; // 写入压缩包 while ((n = is.read()) != -1) { zf.write(n); } zf.close(); os.close(); is.close(); } catch (Exception e) { e.printStackTrace(); } } /** * * @Description 将Socre.zip解压到test文件夹里 * @author HackerD * @CreateAt 2013-5-22 下午7:41:25 * @version V1.0 * @ModificationHistory<BR> * Date Author Version Description <BR> * ---------------------------------------------- * */ public void scoreUnzip() { try { ZipFile zf = new ZipFile("Score.zip"); // 返回指定 ZIP 文件条目。 ZipEntry zEntry = zf.getEntry("Score.txt"); // 返回输入流以读取指定 ZIP 文件条目的内容。 InputStream is = zf.getInputStream(zEntry); OutputStream os = new FileOutputStream("test/Score.txt"); int n = 0; // 解压 while ((n = is.read()) != -1) { os.write(n); } os.close(); is.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { TestZipFile testZipFile = new TestZipFile(); testZipFile.writeData(); testZipFile.findMaxScore(); testZipFile.scoreZip(); testZipFile.scoreUnzip(); } }