工具类 打包文件成压缩包文件

package com.myFirstSpring.test; 
/** 
 * @author tqf
 * @version 创建时间:2019-9-25 下午1:50:02 
 * 类说明:文件打包成压缩包
 */
 
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.util.Date;
import java.util.zip.ZipEntry;  
import java.util.zip.ZipOutputStream;  
 
import com.cloopen.rest.sdk.utils.DateUtil;
 
public class ZipUtil {  
  
    public static void main(String[] args){  
        String sourceFilePath = "D://Qrcoe/";  
        String zipFilePath = "D://Qrcoe/";  
        String fileName =  DateUtil.dateToStr(new Date(),"yyyyMMddHHmmss");
        String fn = ZipUtil.fileToZip(sourceFilePath, zipFilePath, fileName);  
        System.out.println(fn);
    }  
    
    /** 
     * 将存放在sourceFilePath目录下的源文件,打包成fileName名称的zip文件,并存放到zipFilePath路径下 
     * @param sourceFilePath :待压缩的文件路径 
     * @param zipFilePath :压缩后存放路径 
     * @param fileName :压缩后文件的名称 
     * @return 
     */  
    public static String fileToZip(String sourceFilePath,String zipFilePath,String fileName){  
        File sourceFile = new File(sourceFilePath);  
        FileInputStream fis = null;  
        BufferedInputStream bis = null;  
        FileOutputStream fos = null;  
        ZipOutputStream zos = null;  
        //判断源文件是否存在  
        if(!sourceFile.exists()){  
            System.out.println("待压缩的文件目录:"+sourceFilePath+"不存在.");  
        }else{  
            try { 
                File zipFile = new File(zipFilePath + "/" + fileName +".rar");  
                //判断压缩后文件是否会重复
                if(zipFile.exists()){ 
                    System.out.println(zipFilePath + "目录下已存在名字为" + fileName +".rar" +"文件");  
                }else{  
                    
                    //每次生成压缩包之前先删除原有的压缩包
                    zipFile.delete();
                    //获取源文件夹下的所有文件
                    File[] sourceFiles = sourceFile.listFiles();  
                    if(null == sourceFiles || sourceFiles.length<1){  
                        System.out.println("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩.");  
                    }else{  
                        //先删除之前的压缩包文件
                        for(int i=0; i<sourceFiles.length; i++){  
                           if(sourceFiles[i].getName().indexOf(".rar")!=-1){
                               File zipFile2 = new File(sourceFiles[i].getPath());
                               zipFile2.delete();
                               break;
                           }
                        } 
                        //重新获取文件夹所以文件
                        sourceFiles = sourceFile.listFiles();
                        fos = new FileOutputStream(zipFile);  
                        zos = new ZipOutputStream(new BufferedOutputStream(fos));  
                        byte[] bufs = new byte[1024*10];  
                        for(int i=0; i<sourceFiles.length; i++){  
                            //创建ZIP实体,并添加进压缩包  
                            ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());  
                            zos.putNextEntry(zipEntry);  
                            //读取待压缩的文件并写进压缩包里  
                            fis = new FileInputStream(sourceFiles[i]);  
                            bis = new BufferedInputStream(fis, 1024*10);  
                            int read = 0;  
                            while((read=bis.read(bufs, 0, 1024*10)) != -1){  
                                zos.write(bufs,0,read);  
                            }  
                        }  
                    }  
                }  
            } catch (FileNotFoundException e) {  
                e.printStackTrace();  
                throw new RuntimeException(e);  
            } catch (IOException e) {  
                e.printStackTrace();  
                throw new RuntimeException(e);  
            } finally{  
                //关闭流  
                try {  
                    if(null != bis) bis.close();  
                    if(null != zos) zos.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                    throw new RuntimeException(e);  
                }  
            }  
        }  
        return fileName + ".rar";  
    }
      
    
      
}

你可以使用以下代码将HDFS目录下的文件打包压缩包并下载: ```java import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IOUtils; import org.apache.hadoop.io.compress.CompressionCodec; import org.apache.hadoop.io.compress.CompressionOutputStream; import org.apache.hadoop.io.compress.GzipCodec; import java.io.InputStream; import java.io.OutputStream; import java.net.URI; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class HdfsZipDownloadUtil { public static void downloadAsZip(String hdfsPath, String localPath) throws Exception { Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf); Path srcPath = new Path(hdfsPath); ZipOutputStream zos = new ZipOutputStream(fs.create(new Path(localPath))); // 使用压缩算法 CompressionCodec codec = new GzipCodec(); CompressionOutputStream cos = codec.createOutputStream(zos); // 遍历目录下所有文件并压缩 for (Path path : FileUtil.stat2Paths(fs.listStatus(srcPath))) { if (fs.isFile(path)) { String fileName = path.getName(); ZipEntry zipEntry = new ZipEntry(fileName); zos.putNextEntry(zipEntry); InputStream inputStream = fs.open(path); IOUtils.copyBytes(inputStream, cos, conf); inputStream.close(); zos.closeEntry(); } } cos.finish(); cos.close(); zos.close(); } } ``` 其中,`hdfsPath`是要下载的HDFS目录路径,`localPath`是要保存到本地的文件路径。该方法会将目录下所有文件打包 `.zip` 格式的压缩包并保存到本地。你可以按照需要修改压缩算法和保存的文件格式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码奴生来只知道前进~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值