2013-01-18 17 views
6

में एक .tar.gz फ़ाइल निकालें मैं जावा में .tar.gz फ़ाइल निकालने के तरीके के बारे में आवश्यक पैकेज आयात करने या किसी भी ऑनलाइन उदाहरण को प्रतीत नहीं कर सकता।जावा (जेएसपी)

इससे क्या बुरा हो जाता है कि मैं जेएसपी पृष्ठों का उपयोग कर रहा हूं और मुझे अपनी परियोजना में पैकेज आयात करने में परेशानी हो रही है। मैं .jar को WebContent/WEB-INF/lib/ में कॉपी कर रहा हूं और फिर परियोजना पर राइट क्लिक कर रहा हूं और बाहरी जार आयात कर रहा हूं और इसे आयात कर रहा हूं। कभी-कभी संकुल हल होते हैं, अन्य बार वे नहीं करते हैं। ऐसा करने के लिए GZIP को आयात करने के लिए प्रतीत नहीं होता है। जेएसपी के लिए ग्रहण में आयात सहज नहीं हैं जैसे वे सामान्य जावा कोड में हैं जहां आप एक मान्यता प्राप्त पैकेज पर राइट क्लिक कर सकते हैं और आयात का चयन कर सकते हैं।

मैंने अपाचे कॉमन्स लाइब्रेरी, बर्फ और एक अन्य जिसे जेटर कहा जाता है। बर्फ आयात किया गया है, लेकिन मुझे इसका उपयोग करने के तरीके के बारे में कोई उदाहरण नहीं मिल रहा है?

मुझे लगता है कि मुझे पहले gzipped भाग को असंप्रेषित करने की आवश्यकता है, फिर इसे टैरस्ट्रीम के साथ खोलें?

किसी भी मदद की बहुत सराहना की जाती है।

उत्तर

5

ठीक है, मुझे अंततः यह पता चला, अगर यह भविष्य में किसी को भी मदद करता है तो मेरा कोड यहां है। जावा में लिखा गया है, अपाचे कॉमन्स आईओ और कंप्रेसर लाइब्रेरी का उपयोग कर।

File dir = new File("directory/of/.tar.gz/files/here"); 
File listDir[] = dir.listFiles(); 
if (listDir.length!=0){ 
    for (File i:listDir){ 
     /* Warning! this will try and extract all files in the directory 
      if other files exist, a for loop needs to go here to check that 
      the file (i) is an archive file before proceeding */ 
     if (i.isDirectory()){ 
      break; 
     } 
     String fileName = i.toString(); 
     String tarFileName = fileName +".tar"; 
     FileInputStream instream= new FileInputStream(fileName); 
     GZIPInputStream ginstream =new GZIPInputStream(instream); 
     FileOutputStream outstream = new FileOutputStream(tarFileName); 
     byte[] buf = new byte[1024]; 
     int len; 
     while ((len = ginstream.read(buf)) > 0) 
     { 
      outstream.write(buf, 0, len); 
     } 
     ginstream.close(); 
     outstream.close(); 
     //There should now be tar files in the directory 
     //extract specific files from tar 
     TarArchiveInputStream myTarFile=new TarArchiveInputStream(new FileInputStream(tarFileName)); 
     TarArchiveEntry entry = null; 
     int offset; 
     FileOutputStream outputFile=null; 
     //read every single entry in TAR file 
     while ((entry = myTarFile.getNextTarEntry()) != null) { 
      //the following two lines remove the .tar.gz extension for the folder name 
      String fileName = i.getName().substring(0, i.getName().lastIndexOf('.')); 
      fileName = fileName.substring(0, fileName.lastIndexOf('.')); 
      File outputDir = new File(i.getParent() + "/" + fileName + "/" + entry.getName()); 
      if(! outputDir.getParentFile().exists()){ 
       outputDir.getParentFile().mkdirs(); 
      } 
      //if the entry in the tar is a directory, it needs to be created, only files can be extracted 
      if(entry.isDirectory){ 
       outputDir.mkdirs(); 
      }else{ 
       byte[] content = new byte[(int) entry.getSize()]; 
       offset=0; 
       myTarFile.read(content, offset, content.length - offset); 
       outputFile=new FileOutputStream(outputDir); 
       IOUtils.write(content,outputFile); 
       outputFile.close(); 
      } 
     } 
     //close and delete the tar files, leaving the original .tar.gz and the extracted folders 
     myTarFile.close(); 
     File tarFile = new File(tarFileName); 
     tarFile.delete(); 
    } 
} 
16

स्वीकार्य उत्तर ठीक काम करता है, लेकिन मुझे लगता है कि फ़ाइल ऑपरेशन के लिए लिखना अनावश्यक है।

आप की तरह

TarArchiveInputStream tarInput = 
     new TarArchiveInputStream(new GZipInputStream(new FileInputStream("Your file name"))); 

TarArchiveEntry currentEntry = tarInput.getNextTarEntry(); 
while(currentEntry != null) { 
     File f = currentEntry.getFile(); 
     // TODO write to file as usual 
} 

आशा इस मदद कुछ इस्तेमाल कर सकते हैं।

Maven Repo

+1

'फ़ाइल च = currentEntry.getFile();' कैसे आप इस एपीआई AFAIK का उपयोग नहीं है। – Steve

+0

* GZIPInputStream * सभी अपरकेस होना चाहिए – bedrin