ヴィヴェックヴィシャル:
私は、javaの下に使用していますが、それはzip圧縮するとき、それはディレクトリを作成し、そのディレクトリ内のすべての内容をzip圧縮します。EXのために。私は「ディレクトリ」という名前のフォルダを持っていると私は、ZIPファイル内の、ZIP形式のファイルにコンテンツを圧縮したい場合には、フォルダtestZipを作成し、その中のファイルを持っています。私がいない親ディレクトリ内で、zipファイル内のすべてのファイルが必要です。助けてください。他の方法があるかどうか示唆しています。
package folderZip;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipFolder {
public ZipFolder() {
}
public static void main(String[] args) throws Exception {
ZipFolder obj = new ZipFolder();
obj.zipFolder("C:\\Drive\\temp\\testZip","C:\\Drive\\temp\\FolderZiper.zip");
}
public void zipFolder(String srcFolder,
String destZipFile) throws Exception {
ZipOutputStream zip = null;
FileOutputStream fileWriter = null;
fileWriter = new FileOutputStream(destZipFile);
zip = new ZipOutputStream(fileWriter);
addFolderToZip("", srcFolder, zip);
zip.flush();
zip.close();
}
private void addFileToZip(String path, String srcFile,
ZipOutputStream zip) throws Exception {
File folder = new File(srcFile);
if (folder.isDirectory()) {
addFolderToZip(path, srcFile, zip);
} else {
byte[] buf = new byte[1024];
int len;
FileInputStream in = new FileInputStream(srcFile);
zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
while ((len = in.read(buf)) > 0) {
zip.write(buf, 0, len);
}
}
}
private void addFolderToZip(String path, String srcFolder,
ZipOutputStream zip) throws Exception {
File folder = new File(srcFolder);
for (String fileName : folder.list()) {
if (path.equals("")) {
addFileToZip(folder.getName(), srcFolder + "/" + fileName,
zip);
} else {
addFileToZip(path + "/" + folder.getName(),
srcFolder + "/" + fileName, zip);
}
}
}
}
食品のプログラム:
私が正しく理解していればそう、何がしたいことは代わりにZipファイルに登場するフォルダターゲットの、ある、その中のすべてのファイルがZIPファイルに「/」で開始する必要があります。
たとえば、あなたが持っていた場合
FolderToZip/
TestFile1.txt
TestFile2.txt
TestFile3.txt
SomeSubFolder/
TestFile4.txt
TestFile5.txt
TestFile6.txt
Zipファイルの内容が含まれている必要があります
TestFile1.txt
TestFile2.txt
TestFile3.txt
SomeSubFolder/
TestFile4.txt
TestFile5.txt
TestFile6.txt
これを行うには、「スタート」フォルダへの参照を保持して作成するために、あなたが追加されたファイルから外れ、それのパスを削除する必要がありますZipEntry
。
簡単にするために、私がサポートするようにコードを変更するFile
代わりにString
、それはちょうど全体のことをたくさんより少なく厄介になります。
しかし、魔法がここに表示されます...
FileInputStream in = new FileInputStream(srcFile);
String name = srcFile.getPath();
name = name.replace(rootPath.getPath(), "");
System.out.println("Zip " + srcFile + "\n to " + name);
zip.putNextEntry(new ZipEntry(name));
rootPath
初期フォルダ(されC:\\Drive\\temp\\testZip"
、あなたの例では)、srcFile
追加するファイルです。
Runnableを例...
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipFolder {
public ZipFolder() {
}
public static void main(String[] args) throws Exception {
ZipFolder obj = new ZipFolder();
obj.zipFolder(new File("/Users/shanewhitehead/exports"),
new File("FolderZiper.zip"));
}
public void zipFolder(File srcFolder, File destZipFile) throws Exception {
try (FileOutputStream fileWriter = new FileOutputStream(destZipFile);
ZipOutputStream zip = new ZipOutputStream(fileWriter)) {
addFolderToZip(srcFolder, srcFolder, zip);
}
}
private void addFileToZip(File rootPath, File srcFile, ZipOutputStream zip) throws Exception {
if (srcFile.isDirectory()) {
addFolderToZip(rootPath, srcFile, zip);
} else {
byte[] buf = new byte[1024];
int len;
try (FileInputStream in = new FileInputStream(srcFile)) {
String name = srcFile.getPath();
name = name.replace(rootPath.getPath(), "");
System.out.println("Zip " + srcFile + "\n to " + name);
zip.putNextEntry(new ZipEntry(name));
while ((len = in.read(buf)) > 0) {
zip.write(buf, 0, len);
}
}
}
}
private void addFolderToZip(File rootPath, File srcFolder, ZipOutputStream zip) throws Exception {
for (File fileName : srcFolder.listFiles()) {
addFileToZip(rootPath, fileName, zip);
}
}
}
私も使用して、あなたのリソース管理をクリーンアップのtry-と、リソースを