PrinceXR
2014-08-06 11:28
浏览 1.4k
已采纳

解析zip时压缩文件内包含中文名称 ZipInputStream不能支持中文如何解决

图中圈住部分报错,应该就是由于文件名称包含中文导致,网上说需要修改ZipInputStream类,这个方法大神们就不要说了,需要别的方案并且详细一下,分很多可以追加!!!

 

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • yunzhu666 2014-08-07 09:16
    已采纳

    需要导入ant.jar

    [code="java"]import java.io.BufferedInputStream;

    import java.io.BufferedOutputStream;

    import java.io.File;

    import java.io.FileInputStream;

    import java.io.FileNotFoundException;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import java.io.InputStream;

    import java.util.Enumeration;

    import org.apache.tools.zip.ZipEntry;

    import org.apache.tools.zip.ZipFile;

    import org.apache.tools.zip.ZipOutputStream;

    /**

    • 基于Ant的Zip压缩工具类
    • @author 陈峰
      */

      public class AntZipUtils {

      public static final String ENCODING_DEFAULT = "UTF-8";

      public static final int BUFFER_SIZE_DIFAULT = 128;

      public static void makeZip(String[] inFilePaths, String zipPath)

      throws Exception {

      makeZip(inFilePaths, zipPath, ENCODING_DEFAULT);

      }

      public static void makeZip(String[] inFilePaths, String zipPath,

      String encoding) throws Exception {

      File[] inFiles = new File[inFilePaths.length];

      for (int i = 0; i < inFilePaths.length; i++) {

      inFiles[i] = new File(inFilePaths[i]);

      }

      makeZip(inFiles, zipPath, encoding);

      }

      public static void makeZip(File[] inFiles, String zipPath) throws Exception {

      makeZip(inFiles, zipPath, ENCODING_DEFAULT);

      }

      public static void makeZip(File[] inFiles, String zipPath, String encoding)

      throws Exception {

      ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(

      new FileOutputStream(zipPath)));

      zipOut.setEncoding(encoding);

      for (int i = 0; i < inFiles.length; i++) {

      File file = inFiles[i];

      doZipFile(zipOut, file, file.getParent());

      }

      zipOut.flush();

      zipOut.close();

      }

      private static void doZipFile(ZipOutputStream zipOut, File file,

      String dirPath) throws FileNotFoundException, IOException {

      if (file.isFile()) {

      BufferedInputStream bis = new BufferedInputStream(

      new FileInputStream(file));

      String zipName = file.getPath().substring(dirPath.length());

      while (zipName.charAt(0) == '\' || zipName.charAt(0) == '/') {

      zipName = zipName.substring(1);

      }

      ZipEntry entry = new ZipEntry(zipName);

      zipOut.putNextEntry(entry);

      byte[] buff = new byte[BUFFER_SIZE_DIFAULT];

      int size;

      while ((size = bis.read(buff, 0, buff.length)) != -1) {

      zipOut.write(buff, 0, size);

      }

      zipOut.closeEntry();

      bis.close();

      } else {

      File[] subFiles = file.listFiles();

      for (File subFile : subFiles) {

      doZipFile(zipOut, subFile, dirPath);

      }

      }

      }

      public static void unZip(String zipFilePath, String storePath)

      throws IOException {

      unZip(new File(zipFilePath), storePath);

      }

      public static void unZip(File zipFile, String storePath) throws IOException {

      if (new File(storePath).exists()) {

      new File(storePath).delete();

      }

      new File(storePath).mkdirs();

      ZipFile zip = new ZipFile(zipFile);  
      Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zip  
              .getEntries();  
      while (entries.hasMoreElements()) {  
          ZipEntry zipEntry = entries.nextElement();  
          if (zipEntry.isDirectory()) {  
              // TODO  
          } else {  
              String zipEntryName = zipEntry.getName();  
              if (zipEntryName.indexOf(File.separator) > 0) {  
                  String zipEntryDir = zipEntryName.substring(0, zipEntryName  
                          .lastIndexOf(File.separator) + 1);  
                  String unzipFileDir = storePath + File.separator  
                          + zipEntryDir;  
                  File unzipFileDirFile = new File(unzipFileDir);  
                  if (!unzipFileDirFile.exists()) {  
                      unzipFileDirFile.mkdirs();  
                  }  
              }  
      
              InputStream is = zip.getInputStream(zipEntry);  
              FileOutputStream fos = new FileOutputStream(new File(storePath  
                      + File.separator + zipEntryName));  
              byte[] buff = new byte[BUFFER_SIZE_DIFAULT];  
              int size;  
              while ((size = is.read(buff)) > 0) {  
                  fos.write(buff, 0, size);  
              }  
              fos.flush();  
              fos.close();  
              is.close();  
          }  
      }  
      

      }

      public static void main(String[] args) throws Exception {

      String rootDir = "D:\chenfeng";

      String zipPath = "D:\ZipDemo.zip";

      // File[] inFiles = new File(rootDir).listFiles();

      // makeZip(inFiles, zipPath);

      makeZip(new String[] { rootDir }, zipPath);

      unZip(zipPath, "D:\\chenfeng_zip");  
      

      }

      }[/code]

    打赏 评论
  • Iloveyou111111111111 2014-08-06 12:15

    zip可以指定字符编码的
    [code="java"]
    FileOutputStream fos = null;
    ZipOutputStream zos = null;

        try {
            fos = new FileOutputStream(zipFilePath);
            zos = new ZipOutputStream(fos);
            zos.setEncoding("GBK");
            writeZipFile(new File(filePath), zos, "",FileNames);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (zos != null)
                    zos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fos != null)
                    fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    

    [/code]

    打赏 评论
  • yongskyq 2014-08-06 14:52

    建议使用ant包中的api里解压,具体使用可以baidu一下。

    打赏 评论
  • HS_Henry 2014-08-06 16:24

    1:须贴出错误trace,
    2:ZipInputStream的作用
    This class implements an input stream filter for reading files in the
    ZIP file format.
    3:通过2,须确认MultipartFile file所引用的文件时Zip文件类型?

    打赏 评论
  • yunzhu666 2014-08-07 09:15

    建议使用ant的api,jdk原生的api太弱了。

    [size=large]我以前写过工具类,文中上面是基于JDK的压缩工具类,下面是基于ant的压缩工具类:
    [u=/blog/1480293]http://yunzhu.iteye.com/blog/1480293[/u][/size]

    打赏 评论

相关推荐 更多相似问题