반응형

[java]zip파일로 묶어 저장하기

INTRO

 

 

여러 파일들을 zip파일로 묶어서 저장하는 방법입니다.

CONTENTS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public void zipFileDown(String zipFilePath, String zipFileName) {
 
        String[] files = {"    a.png ",
                          "    b.png ",
                          "    c.png ",
                          "    d.png "
                          };
        byte[] buf = new byte[1024];
        
        try {
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFilePath+zipFileName));
            System.out.println("zip파일 생성 성공, 경로+파일 = " +zipFilePath+zipFileName);
         
            for (int i=0; i<files.length; i++) {
                
                FileInputStream in = new FileInputStream(files[i]);
                Path p = Paths.get(files[i]);
                String fileName = p.getFileName().toString();
                
                ZipEntry ze = new ZipEntry(fileName);
                out.putNextEntry(ze);
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                out.closeEntry();
                in.close();
            }
                  
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
}
cs

 

 

 

 

 

 

 

 

 



OUTRO

이 소스를 응용한다면 zip파일로 묶어 다운로드 기능까지 구현한다면 훨씬 멋진 기능이 되겠죠??

반응형

'Programming > Java' 카테고리의 다른 글

[java]tif, tiff파일을 png파일로 변환하기  (0) 2022.09.05
[JAVA]문자열 치환,삭제 replace();  (0) 2022.09.05

+ Recent posts