私は解凍IT-、以下の構造を有している1つのzip形式のフォルダ(例:test.zip)を持っています
WebContent/config/somefiles
WebContent/css/somefile
Webcontent/fonts/somefiles
Webcontent/js/somefiles
WebContent/index.html
今私は、この構造体からのWebContentを削除する必要があります。私の最後の解凍ファイルが持つべきconfig
、css,js
とindex.html
ルートの場所にあります。
その後のWebContentフォルダバイパス拳解凍test.zipに私は、任意の効率的なJavaコードを知っていると、再びWebContentの中に存在するすべてのファイルを圧縮してください。
前もって感謝します!
良いものは、あなたのツールキットに持っているので、これは、一般的なパターン/活動です。
以下、コードを参照してください。であるか、転送されるべきではなく、エントリのドライブ選択に必要に応じてカスタマイズするためにある「選択」方法が、あります:
private static boolean select(String entryName) {
return true; // Customize me
}
ディレクトリが(例えば、「META-INF / MANIFEST.MF」(例えば、「META-INF /」ディレクトリ自体のエントリを持っているだけでなく、ディレクトリ内のファイルのエントリことに注意してください)。スキップディレクトリエントリは、ディレクトリ内のエントリをスキップしません。
注また、これは、ディスクへの解凍を回避することができます。つまり、実行時間に多くを追加するので、避けるべきです。(ただし、エントリ全体がメモリにロードされていないことにも注意してください。データの唯一の単一bufferfullがメモリに今までにあります。)
コードと仮定すると、使い方は「transfer.jar」にあります。
使用法:sample.transfer.TransferSample inputPath outputPath
例えば、それ自身のjarファイルでツールを実行しています:
java -classpath transfer.jar sample.transfer.TransferSample transfer.jar transfer.jar.out
Transfer from [ transfer.jar ]
Transfer to [ transfer.jar.out ]
Transfer from [ c:\dev\transfer\transfer.jar ] (absolute path)
Transfer to [ c:\dev\transfer\transfer.jar.out ] (absolute path)
Entry [ META-INF/MANIFEST.MF ]: Selected
Transferred [ 25 ] bytes
Entry [ sample/ ]: Selected
Entry [ sample/transfer/ ]: Selected
Entry [ sample/transfer/TransferSample.class ]: Selected
Transferred [ 4061 ] bytes
Entry [ sample/transfer/TransferSample.java ]: Selected
Transferred [ 3320 ] bytes
Transfer Success
ここでは、コードは次のようになります。
package sample.transfer;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class TransferSample {
public static void main(String args[]) {
if ( args.length < 2 ) {
System.out.println("Usage: " + TransferSample.class.getName() + " inputPath outputPath");
return;
}
String inputPath = args[0];
String outputPath = args[1];
System.out.println("Transfer from [ " + inputPath + " ]");
System.out.println("Transfer to [ " + outputPath + " ]");
File inputFile = new File(inputPath);
File outputFile = new File(outputPath);
System.out.println("Transfer from [ " + inputFile.getAbsolutePath() + " ] (absolute path)");
System.out.println("Transfer to [ " + outputFile.getAbsolutePath() + " ] (absolute path)");
try ( InputStream inputStream = new FileInputStream(inputFile) ) { // throws IOException
try ( OutputStream outputStream = new FileOutputStream(outputFile) ) { // throws IOException
transfer(inputStream, outputStream);
System.out.println("Transfer Success");
}
} catch ( IOException e ) {
System.err.println("Transfer Failure");
e.printStackTrace(System.err);
}
}
// All static to keep the code simpler.
public static void transfer(InputStream inputStream, OutputStream outputStream) throws IOException {
ZipInputStream zipInputStream = new ZipInputStream(inputStream);
ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
try {
transfer(zipInputStream, zipOutputStream); // throws IOException
} finally {
zipOutputStream.finish(); // throws IOException
}
}
private static final int BUFFER_SIZE = 64 * 1024;
private static boolean select(String entryName) {
return true; // Customize me
}
public static void transfer(ZipInputStream zipInputStream, ZipOutputStream zipOutputStream) throws IOException {
byte[] buffer = new byte[BUFFER_SIZE]; // Single buffer for all transfers
ZipEntry inputEntry;
while ( (inputEntry = zipInputStream.getNextEntry()) != null ) {
String entryName = inputEntry.getName();
if ( !select(entryName) ) {
System.out.println("Entry [ " + entryName + " ]: Not selected");
continue;
} else {
System.out.println("Entry [ " + entryName + " ]: Selected");
}
zipOutputStream.putNextEntry(inputEntry); // throws IOException
if ( !inputEntry.isDirectory() ) {
long bytesTransferred = transfer(zipInputStream, zipOutputStream, buffer); // throws IOException
System.out.println("Transferred [ " + bytesTransferred + " ] bytes");
zipOutputStream.closeEntry(); // throws IOException
}
}
}
private static long transfer(InputStream inputStream, OutputStream outputStream, byte[] buffer) throws IOException {
long totalBytesRead = 0L;
int bytesRead = 0;
while ( (bytesRead = inputStream.read(buffer, 0, buffer.length)) != -1 ) { // throws IOEXception
totalBytesRead += bytesRead;
outputStream.write(buffer, 0, bytesRead);
}
return totalBytesRead;
}
}