Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Stack
- 특수문자 치환
- 정규식
- insert
- select
- 자바
- 리눅스
- 초단위
- 자바스크립트
- 톰캣
- aws elasticache 활용
- catalina.out
- jQuery
- mysql
- Docker Compose
- for문
- architecture
- 제이쿼리
- Linux
- javascript
- sftp
- 만들면서 배우는 클린 아키텍처
- springboot+redis
- 엔티티 코드 치환
- redis + spring boot 함께
- js
- Java
- Entity Code 치환
- Tomcat
- docker 컨테이너로 띄우기
Archives
- Today
- Total
꾸준하게, 차근차근
[Java] 파일 Zip파일로 압축하여 업로드(암호설정) 구현(zip4j 사용) 본문
1. maven dependency 설정
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>1.3.3</version>
</dependency>
zip4j는 람다표현식을 포함하고 있다.
람다표현식은 java 1.8에서 구현되기 때문에 개발환경을 1.8로 설정해주어야 한다.
2. Zipper.java
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipException;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
public class Zipper {
private List<File> files = new ArrayList<>();
/**
* 압축할 파일 추가
*/
public Zipper add(String file) {
return add(new File(file));
}
public Zipper add(File file) {
if( file == null || !file.exists() ) throw new RuntimeException("파일이 존재하지 않습니다.");
files.add(file);
return this;
}
/**
* 압축하기
*
* @param to 생성할 압축파일
* @throws ZipException
*/
public void zip(String to) throws ZipException {
this.zip(to);
}
/**
* 비밀번호 걸어서 압축하기
*
* @param to 생성할 압축파일
* @param password 압축파일 암호
* @throws ZipException
*/
public void zip(String to, String password) throws ZipException {
ZipParameters param = new ZipParameters();
param.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
param.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_ULTRA);
if( password != null) {
param.setEncryptFiles(true);
param.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
param.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
param.setPassword(password);
}
try {
ZipFile zipFile = new ZipFile(to);
if( files.size() == 0 ) throw new RuntimeException("압축대상 파일이 존재하지 않습니다.");
files.forEach(file -> {
try {
if( file.isFile() ) {
zipFile.addFile(file, param);
} else {
zipFile.addFolder(file, param);
}
} catch(Exception e) {
e.printStackTrace();
}
});
} catch (net.lingala.zip4j.exception.ZipException e) {
e.printStackTrace();
}
}
public void unzip(String file) throws ZipException {
this.unzip(file);
}
/**
* 압축 풀기
*
* @param file
* @param password
* @throws ZipException
*/
public void unzip(String file, String password) throws ZipException {
File f = new File(file);
try {
ZipFile zipFile = new ZipFile(f);
if( zipFile.isEncrypted() ) {
zipFile.setPassword(password);
}
zipFile.extractAll(f.getParent());
} catch( Exception e ) {
e.printStackTrace();
}
}
}
3. Zipper 사용
public int makeZipFile(MultipartFile file, HashMap<String, Object> map) {
Zipper zipper = new Zipper();
originFileName = file.getOriginalFilename();
try {
File dir = new File(uploadPath);
// 디렉토리 존재하지 않으면 디렉토리 생성
if( !dir.exists() ) {
dir.mkdirs();
}
File tmpFile = new File(uploadPath, originFileName);
file.transferTo(destFile);
String zipFileName = originFileName.substring(0, originFileName.lastIndexOf(".")) + ".zip";
String password = originFileName.split("_")[0];
zipper.add(tmpFile).zip(uploadPath+"/"+zipFileName, password);
} catch( Exception e ) {
resultCode = -1;
e.printStackTrace();
}
}
🙏 참조 ::
'Java' 카테고리의 다른 글
[Java] List<Integer> To int[] (0) | 2022.04.26 |
---|---|
[Java] SFTP 파일 전송(업로드) 구현(JSch 사용) (0) | 2022.04.26 |
[Java] 배열 내 요소 중복여부 체크 (0) | 2022.04.26 |
[Java] 배열에 특정 값 존재(포함)여부 체크 (0) | 2022.03.19 |
[Java] 정규식(Regular Expression) (0) | 2022.03.19 |