@ -6,7 +6,9 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component ;
import java.io.* ;
import java.time.Year ;
import java.util.* ;
import java.util.stream.Collectors ;
import java.util.zip.ZipEntry ;
import java.util.zip.ZipOutputStream ;
@ -22,34 +24,45 @@ public class ZipMaker {
public void generateZipFile ( List < EPostDto . SendTarget . ImgTarget > zipTargets , CpSetinfo setInfo ) throws IOException {
//todo : 이거 잠시 멈추고 setinfo 쪽 다시 손보자. epostSetinfoService.findSetInfo()
//todo : 여기에 굳이 뎁스 딥하게 나누지말고 필요한 정보들만 하나의 dto로 만들면 될듯 EPostInfo 이거처럼
Objects . requireNonNull ( setInfo , "setInfo is null" ) ;
String path = setInfo . getStrValue8 ( ) + setInfo . getStrValue5 ( ) ;
String cpBackupPath = setInfo . getStrValue10 ( ) ;
//conkey 별로 묶기
String baseDir = setInfo . getStrValue8 ( ) + setInfo . getStrValue5 ( ) ;
String imgDir = setInfo . getStrValue4 ( ) ;
File dir = new File ( baseDir ) ;
if ( ! dir . exists ( ) & & ! dir . mkdirs ( ) ) {
throw new IOException ( "ZIP 저장 디렉토리 생성 실패: " + dir ) ;
}
// ZIP 저장 디렉토리 없으면 생성
File zipFile = new File ( path ) ;
try {
FileOutputStream fos = new FileOutputStream ( zipFile ) ;
ZipOutputStream zos = new ZipOutputStream ( fos ) ;
// 1) CP
for ( EPostDto . SendTarget . ImgTarget target : safeList ( zipTargets ) ) {
List < File > files = findExistingJpegs ( path , target . getMmCode ( ) ) ;
addFilesToZip ( zos , files , target . getConKey ( ) + "/" ) ;
}
Map < String , List < EPostDto . SendTarget . ImgTarget > > byConKey =
safeList ( zipTargets ) . stream ( )
. filter ( t - > t ! = null & & t . getConKey ( ) ! = null & & ! t . getConKey ( ) . isBlank ( ) )
. collect ( Collectors . groupingBy ( EPostDto . SendTarget . ImgTarget : : getConKey , LinkedHashMap : : new , Collectors . toList ( ) ) ) ;
// 2) conKey 그룹 각각 ZIP 생성
for ( Map . Entry < String , List < EPostDto . SendTarget . ImgTarget > > e : byConKey . entrySet ( ) ) {
String conKey = sanitizeForFileName ( e . getKey ( ) ) ;
// 덮어쓰기
File zipFile = new File ( dir , conKey + ".zip" ) ;
// 중복방지
// File zipFile = uniqueZipFile(dir, conKey + ".zip");
try ( FileOutputStream fos = new FileOutputStream ( zipFile ) ;
ZipOutputStream zos = new ZipOutputStream ( fos ) ) {
// // 2) EP
// for (String code : safeList(zipTargets)) {
// List<File> files = findExistingJpegs(epPath, code);
// addFilesToZip(zosEp, files, code + "/");
// }
log . info ( "집 객체추가" ) ;
} catch ( Exception e ) {
log . error ( "ZIP 객체 생성 실패 : " + e . getMessage ( ) ) ;
for ( EPostDto . SendTarget . ImgTarget target : e . getValue ( ) ) {
List < File > files = findExistingJpegs ( setInfo . getStrValue4 ( ) + "/" + target . getMmCode ( ) . substring ( 0 , 5 ) + "/" + Integer . toString ( Year . now ( ) . getValue ( ) ) , target . getMmCode ( ) ) ; // 없으면 빈 리스트
// conKey별 ZIP이므로 내부 폴더 필요 없으면 prefix를 ""로, 필요하면 mmCode 등으로
addFilesToZip ( zos , files , /* zipFolderPrefix= */ "" ) ;
}
log . info ( "ZIP 생성 완료: {} (conKey={}, entries~{})" , zipFile . getAbsolutePath ( ) , conKey , zipFile . length ( ) ) ;
} catch ( Exception ex ) {
log . error ( "ZIP 생성 실패(conKey={}): {}" , conKey , ex . getMessage ( ) , ex ) ;
if ( zipFile . exists ( ) & & zipFile . length ( ) = = 0 L ) zipFile . delete ( ) ;
}
}
}
@ -59,7 +72,7 @@ public class ZipMaker {
if ( isBlank ( baseDir ) | | isBlank ( code ) ) return found ;
for ( String sfx : SUFFIXES ) {
String filename = code + sfx + ".jp e g";
String filename = code + sfx + ".jp g";
File f = new File ( baseDir , filename ) ;
if ( f . isFile ( ) ) {
found . add ( f ) ; // 존재하는 것만 담음 (A/B만 있으면 A,B만)
@ -93,7 +106,21 @@ public class ZipMaker {
return s = = null | | s . trim ( ) . isEmpty ( ) ;
}
// 충돌 시 (1), (2) 붙여서 회피
private static File uniqueZipFile ( File dir , String baseName ) {
File f = new File ( dir , baseName ) ;
int i = 1 ;
int dot = baseName . lastIndexOf ( '.' ) ;
String name = ( dot > 0 ) ? baseName . substring ( 0 , dot ) : baseName ;
String ext = ( dot > 0 ) ? baseName . substring ( dot ) : "" ;
while ( f . exists ( ) ) f = new File ( dir , name + "(" + i + + + ")" + ext ) ;
return f ;
}
// 파일명 안전 처리
private static String sanitizeForFileName ( String s ) {
return s . replaceAll ( "[\\\\/:*?\"<>|]" , "_" ) ;
}