- public class ZipUtil{
- /**
- * 由於有zipModel的原因
- * 故不建議使用static
- * */
- private ZipModel zipModel = new ZipModel();
- /*
- public static void main(String[] args) {
- ZipUtil util = new ZipUtil();
- try {
- util.doZipEnc();
- System.out.println("成功");
- } catch (Exception e) {
- System.out.println("失敗");
- e.printStackTrace();
- }
- }
- public void doZipEnc() {
- try {
- // 將壓縮流寫到內存
- // ZipOutputStream saos = new ZipOutputStream(new ByteArrayOutputStream(), this.zipModel);
- // 本地測試
- FileOutputStream f = new FileOutputStream("C:/Users/Louis/Desktop/test1.zip" ,true);
- ZipOutputStream saos = new ZipOutputStream(f, this.zipModel);
- for(int i=0;i<2;i++){
- ZipParameters parameters = createZipParameters("test" + i + ".txt", "" + i + i + i);
- addNewStreamToZip(saos, ("test" + i + ".txt").getBytes(), parameters);
- }
- // saos.writeTo(f);
- saos.finish();
- saos.close();
- f.close();
- //文件大小
- // System.out.println("size:" +(saos.toByteArray().length/1024));
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- */
- public ZipOutputStream createZipOutputStream(OutputStream outputStream){
- return new ZipOutputStream(outputStream, zipModel);
- }
- public void addNewStreamToZip(ZipOutputStream outputStream, InputStream data, ZipParameters parameters) throws ZipException {
- ByteArrayOutputStream tempOut = new ByteArrayOutputStream();
- try{
- byte[] buffer = new byte[1024];
- for(int i ; (i = data.read(buffer)) != -1 ; ){
- tempOut.write(buffer, 0, i);
- tempOut.flush();
- }
- }catch (Exception e) {
- throw new ZipException(e);
- }finally{
- try{
- data.close();
- tempOut.close();
- }catch (Exception e) {
- }
- }
- addNewStreamToZip(outputStream, tempOut.toByteArray(), parameters);
- }
- public void addNewStreamToZip(ZipOutputStream saos, byte[] data, ZipParameters parameters) throws ZipException {
- try {
- if (zipModel.getEndCentralDirRecord() == null) {
- throw new ZipException("invalid end of central directory record");
- }
- /*
- checkParameters(parameters);
- saos.putNextEntry(null, parameters);
- if (! parameters.getFileNameInZip().endsWith("/") &&
- ! parameters.getFileNameInZip().endsWith("\\")
- ) {
- saos.write(data);
- }
- */
- saos.putNextEntry(null, parameters);
- saos.write(data);
- saos.closeEntry();
- } catch (ZipException e) {
- throw e;
- } catch (Exception e) {
- throw new ZipException(e);
- }
- }
- public ZipParameters createZipParameters(String fileNameInZip, String password){
- ZipParameters parameters = new ZipParameters();
- parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // 壓縮方式
- parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); // 壓縮等級
- parameters.setSourceExternalStream(true);
- parameters.setFileNameInZip(fileNameInZip);
- // 若有多檔,password個別有效
- if(password != null && ! password.trim().isEmpty()) {
- parameters.setEncryptFiles(true);
- parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); // 加密方式
- parameters.setPassword(password);
- }
- return parameters;
- }
- /*
- private void checkParameters(ZipParameters parameters) throws ZipException {
- if (parameters == null) {
- throw new ZipException("cannot validate zip parameters");
- }
- if ((parameters.getCompressionMethod() != Zip4jConstants.COMP_STORE) &&
- parameters.getCompressionMethod() != Zip4jConstants.COMP_DEFLATE) {
- throw new ZipException("unsupported compression type");
- }
- if (parameters.getCompressionMethod() == Zip4jConstants.COMP_DEFLATE) {
- if (parameters.getCompressionLevel() < 0 && parameters.getCompressionLevel() > 9) {
- throw new ZipException("invalid compression level. compression level dor deflate should be in the range of 0-9");
- }
- }
- if (parameters.isEncryptFiles()) {
- if (parameters.getEncryptionMethod() != Zip4jConstants.ENC_METHOD_STANDARD &&
- parameters.getEncryptionMethod() != Zip4jConstants.ENC_METHOD_AES) {
- throw new ZipException("unsupported encryption method");
- }
- if (parameters.getPassword() == null || parameters.getPassword().length <= 0) {
- throw new ZipException("input password is empty or null");
- }
- } else {
- parameters.setAesKeyStrength(-1);
- parameters.setEncryptionMethod(-1);
- }
- }
- */
- }
用法:
- ZipUtil zipUtil = new ZipUtil();
- ZipOutputStream zipOutputStream = zipUtil.createZipOutputStream(outputStream);
- ZipParameters parameters = zipUtil.createZipParameters(fileNameInZip, zipPwd);
- zipUtil.addNewStreamToZip(zipOutputStream, input, parameters);
- zipOutputStream.finish();
- zipOutputStream.close();
第3行這邊可以自由定義需要的OutputStream
第5行
fileNameInZip:在zip檔中檔案的名字
(zip檔中允許相同名字的檔案,有問題的只會發生在解zip的時候一直會問你要不要覆蓋相同的檔案)
zipPwd:密碼是個別有效的
沒有留言:
張貼留言