44import lombok .Getter ;
55import lombok .Setter ;
66import lombok .extern .slf4j .Slf4j ;
7+ import org .apache .commons .compress .archivers .ArchiveEntry ;
8+ import org .apache .commons .compress .archivers .ArchiveInputStream ;
9+ import org .apache .commons .compress .archivers .ArchiveOutputStream ;
10+ import org .apache .commons .compress .archivers .ArchiveStreamFactory ;
11+ import org .apache .commons .compress .archivers .ar .ArArchiveOutputStream ;
12+ import org .apache .commons .compress .archivers .sevenz .SevenZArchiveEntry ;
13+ import org .apache .commons .compress .archivers .sevenz .SevenZFile ;
14+ import org .apache .commons .compress .archivers .sevenz .SevenZOutputFile ;
15+ import org .apache .commons .compress .compressors .CompressorInputStream ;
16+ import org .apache .commons .compress .compressors .CompressorOutputStream ;
17+ import org .apache .commons .compress .compressors .CompressorStreamFactory ;
18+ import org .apache .commons .compress .compressors .gzip .GzipCompressorInputStream ;
19+ import org .apache .commons .compress .compressors .gzip .GzipCompressorOutputStream ;
20+ import org .apache .commons .compress .compressors .gzip .GzipParameters ;
21+ import org .apache .commons .compress .utils .IOUtils ;
22+ import org .apache .commons .compress .utils .SeekableInMemoryByteChannel ;
23+ import org .apache .commons .io .FileUtils ;
24+ import org .apache .commons .io .FilenameUtils ;
25+ import org .apache .commons .lang3 .StringUtils ;
26+
27+ import java .io .ByteArrayOutputStream ;
28+ import java .io .File ;
29+ import java .io .FileInputStream ;
30+ import java .util .ArrayList ;
31+ import java .util .Arrays ;
32+ import java .util .List ;
733
834/**
935 * @ClassName: FileCompressToolService
@@ -22,7 +48,153 @@ public FileCompressToolService(FileCompressToolController fileCompressToolContro
2248 this .fileCompressToolController = fileCompressToolController ;
2349 }
2450
25- public void compressAction () {
51+ public void compressAction () throws Exception {
52+ String zipType = fileCompressToolController .getFileTypeChoiceBox ().getValue ();
53+ String filePath = fileCompressToolController .getSelectFileTextField ().getText ();
54+ String saveFilePath = fileCompressToolController .getSaveFilePathTextField ().getText ();
55+ if (fileCompressToolController .getCompressCheckBox ().isSelected ()) {
56+ byte [] zippedData = null ;
57+ if (new ArchiveStreamFactory ().getOutputStreamArchiveNames ().contains (zipType .toLowerCase ())) {
58+ zippedData = this .zip (filePath , zipType );
59+ } else if (CompressorStreamFactory .getSingleton ().getOutputStreamCompressorNames ().contains (zipType .toLowerCase ())) {
60+ zippedData = this .gzip (filePath , zipType );
61+ } else {
62+ log .warn ("未支持该压缩格式:" + zipType + " fileName:" + filePath );
63+ return ;
64+ }
65+ String [] filePaths = filePath .split ("\\ |" );
66+ List <File > fileList = new ArrayList <>();
67+ for (String path : filePaths ) {
68+ File file = new File (path );
69+ if (file .isDirectory ()) {
70+ fileList .addAll (Arrays .asList (file .listFiles ()));
71+ } else {
72+ fileList .add (file );
73+ }
74+ }
75+ if (StringUtils .isEmpty (saveFilePath )) {
76+ saveFilePath = FilenameUtils .getFullPath (fileList .get (0 ).getPath ());
77+ }
78+ FileUtils .writeByteArrayToFile (new File (saveFilePath , fileList .get (0 ).getName () + "." + zipType ), zippedData );
79+ } else {
80+ if ("AUTO" .equalsIgnoreCase (zipType )) {
81+ try {
82+ zipType = ArchiveStreamFactory .detect (new FileInputStream (new File (filePath )));
83+ } catch (Exception e ) {
84+ try {
85+ zipType = CompressorStreamFactory .detect (new FileInputStream (new File (filePath )));
86+ } catch (Exception e1 ) {
87+ log .warn ("未识别出压缩类型:" + " fileName:" + filePath );
88+ }
89+ }
90+ }
91+ if (new ArchiveStreamFactory ().getInputStreamArchiveNames ().contains (zipType .toLowerCase ())) {
92+ this .unzip (filePath , zipType );
93+ } else if (CompressorStreamFactory .getSingleton ().getInputStreamCompressorNames ().contains (zipType .toLowerCase ())) {
94+ this .ungzip (filePath , zipType );
95+ } else {
96+ log .warn ("未支持该压缩格式:" + zipType + " fileName:" + filePath );
97+ }
98+ }
99+ }
100+
101+ private byte [] zip (String filePath , String zipType ) throws Exception {
102+ String [] filePaths = filePath .split ("\\ |" );
103+ List <File > fileList = new ArrayList <>();
104+ for (String path : filePaths ) {
105+ File file = new File (path );
106+ if (file .isDirectory ()) {
107+ fileList .addAll (Arrays .asList (file .listFiles ()));
108+ } else {
109+ fileList .add (file );
110+ }
111+ }
112+ if (ArchiveStreamFactory .SEVEN_Z .equalsIgnoreCase (zipType )) {
113+ SeekableInMemoryByteChannel seekableByteChannel = new SeekableInMemoryByteChannel ();
114+ SevenZOutputFile sevenZOutput = new SevenZOutputFile (seekableByteChannel );
115+ for (File file : fileList ) {
116+ SevenZArchiveEntry entry = sevenZOutput .createArchiveEntry (file , file .getName ());
117+ sevenZOutput .putArchiveEntry (entry );
118+ sevenZOutput .write (FileUtils .readFileToByteArray (file ));
119+ sevenZOutput .closeArchiveEntry ();
120+ }
121+ sevenZOutput .close ();
122+ return seekableByteChannel .array ();
123+ }
124+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
125+ ArchiveOutputStream archiveOutputStream = new ArchiveStreamFactory ().createArchiveOutputStream (zipType , outputStream );
126+ for (File file : fileList ) {
127+ if (archiveOutputStream instanceof ArArchiveOutputStream ) {
128+ ((ArArchiveOutputStream ) archiveOutputStream ).setLongFileMode (ArArchiveOutputStream .LONGFILE_BSD );
129+ }
130+ ArchiveEntry entry = archiveOutputStream .createArchiveEntry (file , file .getName ());
131+ archiveOutputStream .putArchiveEntry (entry );
132+ archiveOutputStream .write (FileUtils .readFileToByteArray (file ));
133+ archiveOutputStream .closeArchiveEntry ();
134+ }
135+ archiveOutputStream .close ();
136+ return outputStream .toByteArray ();
137+ }
138+
139+ private byte [] gzip (String filePath , String zipType ) throws Exception {
140+ File file = new File (filePath );
141+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
142+ CompressorOutputStream compressorOutputStream ;
143+ if (CompressorStreamFactory .GZIP .equalsIgnoreCase (zipType )) {
144+ GzipParameters gzipParameters = new GzipParameters ();
145+ gzipParameters .setFilename (file .getName ());
146+ compressorOutputStream = new GzipCompressorOutputStream (outputStream , gzipParameters );
147+ } else {
148+ compressorOutputStream = CompressorStreamFactory .getSingleton ().createCompressorOutputStream (zipType , outputStream );
149+ }
150+ compressorOutputStream .write (FileUtils .readFileToByteArray (file ));
151+ compressorOutputStream .close ();
152+ return outputStream .toByteArray ();
153+ }
154+
155+ private void unzip (String filePath , String zipType ) throws Exception {
156+ String saveFilePath = fileCompressToolController .getSaveFilePathTextField ().getText ();
157+ if (StringUtils .isEmpty (saveFilePath )) {
158+ saveFilePath = FilenameUtils .getFullPath (filePath );
159+ }
160+ if (ArchiveStreamFactory .SEVEN_Z .equalsIgnoreCase (zipType )) {
161+ SevenZFile sevenZFile = new SevenZFile (new File (filePath ));
162+ SevenZArchiveEntry entry = null ;
163+ while ((entry = sevenZFile .getNextEntry ()) != null ) {
164+ byte [] bytes = new byte [(int ) entry .getSize ()];
165+ sevenZFile .read (bytes ); // read current entry's data
166+ FileUtils .writeByteArrayToFile (new File (saveFilePath , entry .getName ()), bytes );
167+ }
168+ sevenZFile .close ();
169+ }
170+ ArchiveInputStream archiveInputStream = new ArchiveStreamFactory ().createArchiveInputStream (zipType , new FileInputStream (filePath ));
171+ ArchiveEntry entry = null ;
172+ while ((entry = archiveInputStream .getNextEntry ()) != null ) {
173+ if (!entry .isDirectory ()) {
174+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
175+ IOUtils .copy (archiveInputStream , outputStream );
176+ FileUtils .writeByteArrayToFile (new File (saveFilePath , entry .getName ()), outputStream .toByteArray ());
177+ }
178+ }
179+ archiveInputStream .close ();
180+ }
26181
182+ private void ungzip (String filePath , String zipType ) throws Exception {
183+ String saveFilePath = fileCompressToolController .getSaveFilePathTextField ().getText ();
184+ CompressorInputStream compressorInputStream = CompressorStreamFactory .getSingleton ().createCompressorInputStream (zipType , new FileInputStream (filePath ));
185+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
186+ IOUtils .copy (compressorInputStream , outputStream );
187+ compressorInputStream .close ();
188+ String entryFileName = null ;
189+ if (compressorInputStream instanceof GzipCompressorInputStream ) {
190+ entryFileName = ((GzipCompressorInputStream ) compressorInputStream ).getMetaData ().getFilename ();
191+ }
192+ if (StringUtils .isEmpty (entryFileName )) {
193+ entryFileName = FilenameUtils .getBaseName (filePath );
194+ }
195+ if (StringUtils .isEmpty (saveFilePath )) {
196+ saveFilePath = FilenameUtils .getFullPath (filePath );
197+ }
198+ FileUtils .writeByteArrayToFile (new File (saveFilePath , entryFileName ), outputStream .toByteArray ());
27199 }
28200}
0 commit comments