java ftp解压文件

I_color 发布于 2016/05/12 09:14
阅读 715
收藏 0

有个需求,通过ftp交互文件,对方把文件压缩上传到ftp上

我这边怎么进行解压

有没有做java的大神指点下

加载中
0
Eric_林
Eric_林
import java.util.zip.*;
import java.io.*;

public class UnzipTest {
     public static void main(String[] args) {
          if (args.length != 1) {
               System.out.println("请输入正确参数:java UnzipTest 需解压的文件(e.g. d:/test.zip)");
          } else {
               Unzip unzip = new Unzip();
               if (unzip.unzip(args[0])) {
                    System.out.println("文件解压成功。");
               } else {
                    System.out.println("文件解压失败。");
               }
          }    
     }
}

class Unzip {
     public Unzip() {}

     /*
     * @param srcZipFile 需解压的文件名
     * @return  如果解压成功返回true
     */
     public boolean unzip(String srcZipFile) {
          boolean isSuccessful = true;
          try {
               BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcZipFile));
               ZipInputStream zis = new ZipInputStream(bis);

               BufferedOutputStream bos = null;

               //byte[] b = new byte[1024];
               ZipEntry entry = null;
               while ((entry=zis.getNextEntry()) != null) {
                    String entryName = entry.getName();
                    bos = new BufferedOutputStream(new FileOutputStream("d:/" + entryName));
                    int b = 0;
                    while ((b = zis.read()) != -1) {
                         bos.write(b);
                    }
                    bos.flush();
                    bos.close();
               }
               zis.close();
          } catch (IOException e) {
               isSuccessful = false;
          }
          return isSuccessful;
     }
}



I_color
I_color
谢谢哥们
0
帰郷
帰郷
public static boolean unZip(File zipFile, String outputDir) {
		ZipFile zip = null;
		try {
			zip = new ZipFile(zipFile);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		for (Enumeration entries = zip.entries(); entries.hasMoreElements();) {
			ZipEntry entry = (ZipEntry) entries.nextElement();
			String zipEntryName = new String(entry.getName().getBytes(), Charset.forName("GBK")).replace("\\", "/");			
			if(entry.isDirectory()) {
				File file = new File(outputDir + zipEntryName);
				if (!file.exists() || !file.isDirectory())
					file.mkdirs();
				continue;
			}
			OutputStream out = null;
			InputStream in = null;
			try {
				out = new FileOutputStream(outputDir + zipEntryName);
				in = zip.getInputStream(entry);
				byte[] buf1 = new byte[1024*4];
				int len = 0;
				while ((len = in.read(buf1)) > 0) {
					out.write(buf1, 0, len);
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					if(in != null){
						in.close();
					}
					if(out != null){
						out.close();
					}
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
		}
		System.out.println("-----解压完成-----");
		return true;
	}



I_color
I_color
谢谢
返回顶部
顶部