⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zipper.java

📁 简单的生成一个图片
💻 JAVA
字号:
package 压缩文件夹.OO重构;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class Zipper 
{
	public void ya(String[] filenames,String outFilename)
	{
  
	    // Create a buffer for reading the files
	    byte[] buf = new byte[1024];
	    
	    try 
	    {
	        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));
	    
	        // Compress the files
	        for (int i=0; i<filenames.length; i++) {
	            FileInputStream in = new FileInputStream(filenames[i]);
	    
	            // Add ZIP entry to output stream.
	            out.putNextEntry(new ZipEntry(filenames[i]));
	    
	            // Transfer bytes from the file to the ZIP file
	            int len;
	            while ((len = in.read(buf)) > 0) {
	                out.write(buf, 0, len);
	            }
	    
	            // Complete the entry
	            out.closeEntry();
	            in.close();
	        }
	    
	        // Complete the ZIP file
	        out.close();
	    } catch (IOException e) {
	    }

	}
	
	public String[]  lieWenJian(String dirName)
	{
	    File dir = new File(dirName);
	    
	    String[] children = dir.list();
	    if (children == null)
	    {
	        // Either dir does not exist or is not a directory
	    }
	    else
	    {
	    	for (int i = 0; i < children.length; i++) 
	    	{
				children[i]=dirName+"\\"+children[i];
			}
	    	return  children;
	    }
	    return null;
	}
	
	public   List<String>   lieZipWenJian(String zipName)
	{
	    try 
	    {
	        ZipFile zf = new ZipFile(zipName);
	    
	        List<String> list=new ArrayList<String>();
	        for (Enumeration entries = zf.entries(); entries.hasMoreElements();) 
	        {
	            String zipEntryName = ((ZipEntry)entries.nextElement()).getName();
	            list.add(zipEntryName);
	        }
	        return list;
	    } 
	    catch (IOException e) 
	    {
	    }	
	    return null;
	}
	
	public void jie(String zipName, List<String>  listWenJian)
	{
	    try 
	    {
	        // Open the ZIP file
	        String inFilename =zipName;
	        ZipInputStream in = new ZipInputStream(new FileInputStream(inFilename));
	    
	        // Get the first entry
	        for (int i = 0; i < listWenJian.size(); i++)
	        {
		        ZipEntry entry = in.getNextEntry();
		    
		        // Open the output file
		        String outFilename = listWenJian.get(i);
		        OutputStream out = new FileOutputStream(outFilename);
		    
		        // Transfer bytes from the ZIP file to the output file
		        byte[] buf = new byte[1024];
		        int len;
		        while ((len = in.read(buf)) > 0) 
		        {
		            out.write(buf, 0, len);
		        }
		    
		        // Close the streams
		        out.close();				
			}	

		    in.close();				

	    } 
	    catch (IOException e)
	    {
	    	System.out.println(e.getMessage());
	    }		
	}
	
	public static void main(String[] args) 
	{
		Zipper zp=new Zipper();

//		String[] filenames= zp.lieWenJian("c:\\aa");		
//		zp.ya(filenames, "c:\\bb.zip");
		String zipName="c:\\bb.zip";
		List<String> listWenJian=zp.lieZipWenJian(zipName);
		zp.jie(zipName, listWenJian);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -