程式碼自留地:小檔案合併成大檔案,需要配置BytesZip使用,java
阿新 • • 發佈:2018-12-10
public class FileZip implements Serializable { String fileName = null; byte [] zipBytes = null; public FileZip(){ } public FileZip(String fileName,byte [] zipBytes){ this.fileName = fileName; this.zipBytes = zipBytes; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public byte[] getZipBytes() { return zipBytes; } public void setZipBytes(byte[] zipBytes) { this.zipBytes = zipBytes; } }
public class Test { public static void main1(String args []) throws IOException{ ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test.z")); for(File file : new File("C:\\Users\\Administrator\\Desktop\\test").listFiles()){ out.writeObject(zip(file)); } out.close(); System.out.println("完成."); } public static void main(String args []) throws IOException, ClassNotFoundException{ File zFile = new File("C:\\Users\\Administrator\\Desktop\\test.z"); File unzipDir = new File("C:\\Users\\Administrator\\Desktop\\test2"); if(!unzipDir.isDirectory()) unzipDir.mkdirs(); ObjectInputStream in = new ObjectInputStream(new FileInputStream(zFile)); Object obj = null; try{ while(null != (obj = in.readObject())){ FileZip zipObj = (FileZip)obj; String fileName = zipObj.getFileName(); byte zipBytes [] = zipObj.getZipBytes(); byte bytes [] = BytesZip.unZip(zipBytes); File unzipFile = new File(unzipDir,fileName); FileUtils.writeByteArrayToFile(unzipFile, bytes); } }catch(EOFException EOFE){ } in.close(); } public static FileZip zip(File file) throws IOException{ String fileName = file.getName(); byte [] bytes = FileUtils.readFileToByteArray(file); byte [] zipBytes = BytesZip.zip(bytes); return new FileZip(fileName,zipBytes); } }