วิธีสร้าง .zip หรือ .tar โดยทางโปรแกรมใน Java โดยใช้ Apache Commons Archivers and Compressors
เผยแพร่แล้ว: 2016-03-08พื้นฐาน Apache โดยค่าเริ่มต้นมาพร้อมกับยูทิลิตี้มากมายให้เราใช้ ในกรณีส่วนใหญ่ เราไม่ทราบถึงยูทิลิตี้ที่มีอยู่เพื่อใช้ในสภาพแวดล้อมการผลิตของเรา
ในบทช่วยสอนนี้ เราจะพูดถึงยูทิลิตี้ตัวใดตัวหนึ่งที่เราสามารถบีบอัดไฟล์หรือไดเร็กทอรีใดๆ โดยทางโปรแกรมใน Java กล่าวอีกนัยหนึ่งยูทิลิตี้เก็บถาวรอย่างง่าย
ทำไมเราต้องการยูทิลิตี้นี้?
ครั้งหนึ่งฉันเคยเขียนบทความเกี่ยวกับวิธีการอัปโหลดไฟล์โดยใช้สถาปัตยกรรม Spring MVC หากคุณมีไฟล์ขนาดใหญ่มากและคุณกำลังโฮสต์ไฟล์ผู้ใช้รายอื่นในระบบไฟล์บางระบบ เช่น netapp หรือ filer หรืออื่น ๆ คุณอาจต้องบีบอัดไฟล์ก่อนอัปโหลด คุณสามารถแต่งงานกับโค้ดด้านล่างในใบสมัครของคุณเพื่อบรรลุวัตถุประสงค์เดียวกัน
มาเช็คเอาต์ผลลัพธ์กันก่อนเพื่อให้เข้าใจมากขึ้น:
ก่อน:
หลังจาก:
มาเริ่มกันเลย:
- สร้างคลาส
CrunchifyCompressArchivesUtility.java
- เพิ่มการพึ่งพา maven ด้านล่างให้กับโครงการของคุณ
- หากคุณไม่มีโปรเจ็กต์ maven ให้ทำตามขั้นตอนเหล่านี้
1 2 3 4 5 |
< dependency > < groupId > org . apache . commons < / groupId > < artifactId > commons - compress < / artifactId > < version > 1.9 < / version > < / dependency > |
- เราจะใช้ยูทิลิตี้
compress archivers
Apache Commons - คลาส
TarArchiveEntry
แสดงถึงรายการในไฟล์เก็บถาวร Tar ประกอบด้วยส่วนหัวของรายการ เช่นเดียวกับไฟล์ของรายการ รายการสามารถสร้างอินสแตนซ์ได้ด้วยวิธีใดวิธีหนึ่งจากสามวิธี ขึ้นอยู่กับว่าจะใช้งานอย่างไร - คลาส
TarArchiveOutputStream
เขียนไฟล์ tar ของ UNIX เป็นไฟล์ OutputStream - เราจะเก็บถาวรเพียงไฟล์ก่อน
- นอกจากนี้ในโปรแกรมเดียวกันเราจะเก็บไดเร็กทอรี
- เราจะแปลงไฟล์และไดเร็กทอรีเป็นไฟล์
.zip
หากคุณต้องการ ..tar
ก็แค่เปลี่ยนรหัสด้านล่าง - กรุณาเปลี่ยนเส้นทางในรหัสด้านล่าง
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
package com . crunchify . tutorials ; import java . io . BufferedInputStream ; import java . io . File ; import java . io . FileInputStream ; import java . io . FileOutputStream ; import org . apache . commons . compress . archivers . tar . TarArchiveEntry ; import org . apache . commons . compress . archivers . tar . TarArchiveOutputStream ; /** * @author Crunchify.com * */ public class CrunchifyCompressArchivesUtility { private static final String CRUNCHIFY_BASEDIR = "" ; // Default output path private static final String CRUNCHIFY_PATH = "/Users/<username>/Desktop/" ; // .zip or .tar as per need private static final String FILE_EXTENSION = ".zip" ; public static void main ( String [ ] args ) { try { // Archive File crunchfyArchive ( "/Users/appshah/Desktop/crunchifyTarFile.txt" ) ; log ( "Archive a file task completed...\n" ) ; // Archive Directory crunchfyArchive ( "/Users/appshah/Desktop/crunchifyTarDirectory" ) ; log ( "Archive a Directory task completed..." ) ; } catch ( Exception e ) { log ( e . getStackTrace ( ) . toString ( ) ) ; } } public static void crunchfyArchive ( String srcPath ) throws Exception { File crunchifySourceFile = new File ( srcPath ) ; // Returns the name of the file or directory denoted by this abstract pathname String crunchifyFileName = crunchifySourceFile . getName ( ) ; // Returns the pathname string of this abstract pathname's parent String crunchifyBaseFileNamePath = crunchifySourceFile . getParent ( ) ; String destPath = crunchifyBaseFileNamePath + File . separator + crunchifyFileName + FILE_EXTENSION ; log ( "Archived Location: " + destPath ) ; TarArchiveOutputStream outputStream = new TarArchiveOutputStream ( new FileOutputStream ( new File ( destPath ) ) ) ; crunchfyArchive ( crunchifySourceFile , outputStream , CRUNCHIFY_BASEDIR ) ; // Flushes this output stream and forces any buffered output bytes to be written out outputStream . flush ( ) ; // Closes the underlying OutputStream outputStream . close ( ) ; } private static void crunchfyArchive ( File crunchifySourceFile , TarArchiveOutputStream outputStream , String crunchifyBasePath ) throws Exception { if ( crunchifySourceFile . isDirectory ( ) ) { // Archive Directory archiveCrunchifyDirectory ( crunchifySourceFile , outputStream , crunchifyBasePath ) ; } else { // Archive File archiveCrunchifyFile ( crunchifySourceFile , outputStream , crunchifyBasePath ) ; } } private static void archiveCrunchifyDirectory ( File crunchifyDirectory , TarArchiveOutputStream outputStream , String crunchifyBasePath ) throws Exception { // Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname File [ ] crunchifyFiles = crunchifyDirectory . listFiles ( ) ; if ( crunchifyFiles ! = null ) { if ( crunchifyFiles . length < 1 ) { // Construct an entry with only a name. This allows the programmer to construct the entry's header "by hand". File // is set to null TarArchiveEntry entry = new TarArchiveEntry ( crunchifyBasePath + crunchifyDirectory . getName ( ) + CRUNCHIFY_PATH ) ; // Put an entry on the output stream outputStream . putArchiveEntry ( entry ) ; // Close an entry. This method MUST be called for all file entries that contain data outputStream . closeArchiveEntry ( ) ; } // Repeat for all files for ( File crunchifyFile : crunchifyFiles ) { crunchfyArchive ( crunchifyFile , outputStream , crunchifyBasePath + crunchifyDirectory . getName ( ) + CRUNCHIFY_PATH ) ; } } } private static void archiveCrunchifyFile ( File crunchifyFile , TarArchiveOutputStream outputStream , String crunchifyDirectory ) throws Exception { TarArchiveEntry crunchifyEntry = new TarArchiveEntry ( crunchifyDirectory + crunchifyFile . getName ( ) ) ; // Set this entry's file size crunchifyEntry . setSize ( crunchifyFile . length ( ) ) ; outputStream . putArchiveEntry ( crunchifyEntry ) ; BufferedInputStream inputStream = new BufferedInputStream ( new FileInputStream ( crunchifyFile ) ) ; int counter ; // 512: buffer size byte byteData [ ] = new byte [ 512 ] ; while ( ( counter = inputStream . read ( byteData , 0 , 512 ) ) ! = - 1 ) { outputStream . write ( byteData , 0 , counter ) ; } inputStream . close ( ) ; outputStream . closeArchiveEntry ( ) ; } // Crunchify's favorite log utility private static void log ( String string ) { System . out . println ( string ) ; } } |
เอาท์พุต

1 2 3 4 5 |
Archived Location: /Users/<username>/Desktop/crunchifyTarFile.txt.zip Archive a file task completed... Archived Location: /Users/<username>/Desktop/crunchifyTarDirectory.zip Archive a Directory task completed... |