java zip打包與亂碼的解決

關於 java zip 打包用的場景仍是有很多,包括在歸檔的時候,還有就是在提交數據的時候,通常都會採用這種方式。可是這裏面最麻煩的就是java亂碼的問題。

解決亂碼的問題主要是引用apache的包ant.jar。而後注意下面紅色代碼部分,java自帶的包中也有這兩個類。若是亂碼最好是採用apache的這兩個類。這樣纔會有設置編碼的 zos.setEncoding("gbk") 這個方法。這樣就可解決java zip 打包的知碼問題。



下面是演示的 java代碼:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipFile;

//這裏注意不能採用java下面的類包,否則沒有設置編碼的方法

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
html


/**
* @Copyright @ 2012 All right reserved
* @version 建立時間:Created on 2012-12-19
* @author 做者:Create by www.360buyli.com
* @Email : 360buyli@gmail.com
* @description eclipse註釋模板
*/ java

public class ZipUtil { apache

public static final String ZIP_FILENAME = ""; // 須要解壓縮的文件名
public static final String ZIP_DIR = ""; // 須要壓縮的文件夾
public static final String UN_ZIP_DIR = ""; // 要解壓的文件目錄
public static final int BUFFER = 1024; // 緩存大小 緩存



public static void main(String[] args) {
  try {
   zipFile("F:\\page","F:\\page\\demo.zip");
  } catch (Exception e) {
   e.printStackTrace();
  }
}

public static void zipFile(String baseDir, String fileName)
   throws Exception {
  List fileList = getSubFiles(new File(baseDir));
  ZipOutputStream zos = new ZipOutputStream(
    new FileOutputStream(fileName));
  ZipEntry ze = null;
  byte[] buf = new byte[BUFFER];
  int readLen = 0;
  for (int i = 0; i < fileList.size(); i++) {
   File f = (File) fileList.get(i);
   ze = new ZipEntry(getAbsFileName(baseDir, f));
   ze.setSize(f.length());
  
   ze.setTime(f.lastModified());
   zos.putNextEntry(ze);
   InputStream is = new BufferedInputStream(new FileInputStream(f));
   while ((readLen = is.read(buf, 0, BUFFER)) != -1) {
    zos.write(buf, 0, readLen);
   }
   is.close();
  }
  zos.setEncoding("gbk");
  zos.close();
} eclipse

private static String getAbsFileName(String baseDir, File realFileName) {
  File real = realFileName;
  File base = new File(baseDir);
  String ret = real.getName();
  while (true) {
   real = real.getParentFile();
   if (real == null)
    break;
   if (real.equals(base))
    break;
   else
    ret = real.getName() + "/" + ret;
  }
  return ret;
} 編碼

private static List getSubFiles(File baseDir) {
  List ret = new ArrayList();
  File[] tmp = baseDir.listFiles();
  for (int i = 0; i < tmp.length; i++) {
   if (tmp[i].isFile())
    ret.add(tmp[i]);
   if (tmp[i].isDirectory())
    ret.addAll(getSubFiles(tmp[i]));
  }
  return ret;
} spa

public static void upZipFile() throws Exception {
  ZipFile zfile = new ZipFile(ZIP_FILENAME);
  Enumeration zList = zfile.entries();
  ZipEntry ze = null;
  byte[] buf = new byte[1024];
  while (zList.hasMoreElements()) {
   ze = (ZipEntry) zList.nextElement();
   if (ze.isDirectory()) {
    File f = new File(ZIP_DIR + ze.getName());
    f.mkdir();
    continue;
   }
   OutputStream os = new BufferedOutputStream(new FileOutputStream(
     getRealFileName(ZIP_DIR, ze.getName())));
   InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
   int readLen = 0;
   while ((readLen = is.read(buf, 0, 1024)) != -1) {
    os.write(buf, 0, readLen);
   }
   is.close();
   os.close();
  }
  zfile.close();
} .net

public static File getRealFileName(String baseDir, String absFileName) {
  String[] dirs = absFileName.split("/");
  File ret = new File(baseDir);
  if (dirs.length > 1) {
   for (int i = 0; i < dirs.length - 1; i++) {
    ret = new File(ret, dirs[i]);
   }
   if (!ret.exists())
    ret.mkdirs();
   ret = new File(ret, dirs[dirs.length - 1]);
   return ret;
  }
  return ret;
} htm

public static void deleteDirFile(String path) {
  File file = new File(path);
  if (file.isDirectory()) { // 若是是目錄,先遞歸刪除
   String[] list = file.list();
   for (int i = 0; i < list.length; i++) {
    deleteDirFile(path + "\\" + list[i]); // 先刪除目錄下的文件
   }
  }
  file.delete();
} 遞歸

public static String newFolder(String dir) {
  java.io.File myFilePath = new java.io.File(dir);
  if (myFilePath.isDirectory()) {
  } else {
   myFilePath.mkdirs();
  }
  return dir;
}

public static String getFileNames(String path) {

  File file = new File(path); // get file list where the path has
  File[] array = file.listFiles(); // 得到文件列表
  String pdfNames = "";

  for (int i = 0; i < array.length; i++) {
   if (array[i].isFile()) {
    if (array[i].getName().endsWith(".pdf")) { // 得到pdf文件名稱
     pdfNames += array[i].getName().substring(0,
       array[i].getName().length() - 4)
       + ",";
    }
   }
  }
  if (pdfNames.length() > 0) {
   pdfNames.substring(0, pdfNames.length() - 1);
  }
  return pdfNames;
}

public static void copyFile(String oldPath, String newPath) {
  try {
   int bytesum = 0;
   int byteread = 0;
   File oldfile = new File(oldPath);
   if (oldfile.exists()) { // 文件存在時
    InputStream inStream = new FileInputStream(oldPath); // 讀入原文件
    FileOutputStream fs = new FileOutputStream(newPath);
    byte[] buffer = new byte[1444];
    while ((byteread = inStream.read(buffer)) != -1) {
     bytesum += byteread; // 字節數文件大小
     fs.write(buffer, 0, byteread);
    }
    inStream.close();
   }
  } catch (Exception e) {
   System.out.println("copy file error!");
   e.printStackTrace();
  }
}

public static boolean fileExist(String fileNames, String pdfName) {
  boolean flag = false;
  if (!"".equals(fileNames)) {
   String[] nameArr = fileNames.split(",");
   for (int i = 0; i < nameArr.length; i++) {
    if (pdfName.equals(nameArr[i])) { // 若是文件名相同則執行拷貝操做(拷貝到zip目錄準備打包)
     flag = true;
     break;
    }
   }
  }
  return flag;
}

public static void deleteFileAndDir(String path) {
  File file = new File(path);
  File[] array = file.listFiles();
  for (int i = 0; i < array.length; i++) {
   if (array[i].isFile()) {
    array[i].delete();
   } else if (array[i].isDirectory()) {
    deleteDirFile(array[i].getPath());
   }
  }
}

}關於java zip 打包用的場景仍是有很多,包括在歸檔的時候,還有就是在提交數據的時候,通常都會採用這種方式。可是這裏面最麻煩的就是java亂碼的問題。

解決亂碼的問題主要是引用apache的包ant.jar。而後注意下面紅色代碼部分,java自帶的包中也有這兩個類。若是亂碼最好是採用apache的這兩個類。這樣纔會有設置編碼的 zos.setEncoding("gbk")這個方法。這樣就可解決java zip 打包的知碼問題。



下面是演示的java代碼:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipFile;

//這裏注意不能採用java下面的類包,否則沒有設置編碼的方法

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;


/**
* @Copyright @ 2012 All right reserved
* @version 建立時間:Created on 2012-12-19
* @author 做者:Create by www.360buyli.com
* @Email : 360buyli@gmail.com
* @description eclipse註釋模板
*/

public class ZipUtil {

public static final String ZIP_FILENAME = ""; // 須要解壓縮的文件名
public static final String ZIP_DIR = ""; // 須要壓縮的文件夾
public static final String UN_ZIP_DIR = ""; // 要解壓的文件目錄
public static final int BUFFER = 1024; // 緩存大小



public static void main(String[] args) {
  try {
   zipFile("F:\\page","F:\\page\\demo.zip");
  } catch (Exception e) {
   e.printStackTrace();
  }
}

public static void zipFile(String baseDir, String fileName)
   throws Exception {
  List fileList = getSubFiles(new File(baseDir));
  ZipOutputStream zos = new ZipOutputStream(
    new FileOutputStream(fileName));
  ZipEntry ze = null;
  byte[] buf = new byte[BUFFER];
  int readLen = 0;
  for (int i = 0; i < fileList.size(); i++) {
   File f = (File) fileList.get(i);
   ze = new ZipEntry(getAbsFileName(baseDir, f));
   ze.setSize(f.length());
  
   ze.setTime(f.lastModified());
   zos.putNextEntry(ze);
   InputStream is = new BufferedInputStream(new FileInputStream(f));
   while ((readLen = is.read(buf, 0, BUFFER)) != -1) {
    zos.write(buf, 0, readLen);
   }
   is.close();
  }
  zos.setEncoding("gbk");
  zos.close();
}

private static String getAbsFileName(String baseDir, File realFileName) {
  File real = realFileName;
  File base = new File(baseDir);
  String ret = real.getName();
  while (true) {
   real = real.getParentFile();
   if (real == null)
    break;
   if (real.equals(base))
    break;
   else
    ret = real.getName() + "/" + ret;
  }
  return ret;
}

private static List getSubFiles(File baseDir) {
  List ret = new ArrayList();
  File[] tmp = baseDir.listFiles();
  for (int i = 0; i < tmp.length; i++) {
   if (tmp[i].isFile())
    ret.add(tmp[i]);
   if (tmp[i].isDirectory())
    ret.addAll(getSubFiles(tmp[i]));
  }
  return ret;
}

public static void upZipFile() throws Exception {
  ZipFile zfile = new ZipFile(ZIP_FILENAME);
  Enumeration zList = zfile.entries();
  ZipEntry ze = null;
  byte[] buf = new byte[1024];
  while (zList.hasMoreElements()) {
   ze = (ZipEntry) zList.nextElement();
   if (ze.isDirectory()) {
    File f = new File(ZIP_DIR + ze.getName());
    f.mkdir();
    continue;
   }
   OutputStream os = new BufferedOutputStream(new FileOutputStream(
     getRealFileName(ZIP_DIR, ze.getName())));
   InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
   int readLen = 0;
   while ((readLen = is.read(buf, 0, 1024)) != -1) {
    os.write(buf, 0, readLen);
   }
   is.close();
   os.close();
  }
  zfile.close();
}

public static File getRealFileName(String baseDir, String absFileName) {
  String[] dirs = absFileName.split("/");
  File ret = new File(baseDir);
  if (dirs.length > 1) {
   for (int i = 0; i < dirs.length - 1; i++) {
    ret = new File(ret, dirs[i]);
   }
   if (!ret.exists())
    ret.mkdirs();
   ret = new File(ret, dirs[dirs.length - 1]);
   return ret;
  }
  return ret;
}

public static void deleteDirFile(String path) {
  File file = new File(path);
  if (file.isDirectory()) { // 若是是目錄,先遞歸刪除
   String[] list = file.list();
   for (int i = 0; i < list.length; i++) {
    deleteDirFile(path + "\\" + list[i]); // 先刪除目錄下的文件
   }
  }
  file.delete();
}

public static String newFolder(String dir) {
  java.io.File myFilePath = new java.io.File(dir);
  if (myFilePath.isDirectory()) {
  } else {
   myFilePath.mkdirs();
  }
  return dir;
}

public static String getFileNames(String path) {

  File file = new File(path); // get file list where the path has
  File[] array = file.listFiles(); // 得到文件列表
  String pdfNames = "";

  for (int i = 0; i < array.length; i++) {
   if (array[i].isFile()) {
    if (array[i].getName().endsWith(".pdf")) { // 得到pdf文件名稱
     pdfNames += array[i].getName().substring(0,
       array[i].getName().length() - 4)
       + ",";
    }
   }
  }
  if (pdfNames.length() > 0) {
   pdfNames.substring(0, pdfNames.length() - 1);
  }
  return pdfNames;
}

public static void copyFile(String oldPath, String newPath) {
  try {
   int bytesum = 0;
   int byteread = 0;
   File oldfile = new File(oldPath);
   if (oldfile.exists()) { // 文件存在時
    InputStream inStream = new FileInputStream(oldPath); // 讀入原文件
    FileOutputStream fs = new FileOutputStream(newPath);
    byte[] buffer = new byte[1444];
    while ((byteread = inStream.read(buffer)) != -1) {
     bytesum += byteread; // 字節數文件大小
     fs.write(buffer, 0, byteread);
    }
    inStream.close();
   }
  } catch (Exception e) {
   System.out.println("copy file error!");
   e.printStackTrace();
  }
}

public static boolean fileExist(String fileNames, String pdfName) {
  boolean flag = false;
  if (!"".equals(fileNames)) {
   String[] nameArr = fileNames.split(",");
   for (int i = 0; i < nameArr.length; i++) {
    if (pdfName.equals(nameArr[i])) { // 若是文件名相同則執行拷貝操做(拷貝到zip目錄準備打包)
     flag = true;
     break;
    }
   }
  }
  return flag;
}

public static void deleteFileAndDir(String path) {
  File file = new File(path);
  File[] array = file.listFiles();
  for (int i = 0; i < array.length; i++) {
   if (array[i].isFile()) {
    array[i].delete();
   } else if (array[i].isDirectory()) {
    deleteDirFile(array[i].getPath());
   }
  }
}

}

相關文章
相關標籤/搜索