gpt4 book ai didi

java - 在 Windows 中创建 Zip 文件并在 Linux 中提取 Zip 文件

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:35:14 26 4
gpt4 key购买 nike

我在 Windows 下创建了一个 zip 文件(连同目录)如下(代码来自 http://www.exampledepot.com/egs/java.util.zip/CreateZip.html):

package sandbox;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
*
* @author yan-cheng.cheok
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// These are the files to include in the ZIP file
String[] filenames = new String[]{"MyDirectory" + File.separator + "MyFile.txt"};

// Create a buffer for reading the files
byte[] buf = new byte[1024];

try {
// Create the ZIP file
String outFilename = "outfile.zip";
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));

// Compress the files
for (int i=0; i<filenames.length; i++) {
FileInputStream in = new FileInputStream(filenames[i]);

// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(filenames[i]));

// Transfer bytes from the file to the ZIP file
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}

// Complete the entry
out.closeEntry();
in.close();
}

// Complete the ZIP file
out.close();
} catch (IOException e) {
e.printStackTrace();
}

}

}

可以在 Windows 下使用 http://www.exampledepot.com/egs/java.util.zip/GetZip.html 毫无问题地提取新创建的 zip 文件。

但是,我意识到如果我在 Linux 下使用 http://www.exampledepot.com/egs/java.util.zip/GetZip.html 的修改版本提取新创建的 zip 文件.原始版本不使用 zipEntry.isDirectory() 检查目录。

public static boolean extractZipFile(File zipFilePath, boolean overwrite) {
InputStream inputStream = null;
ZipInputStream zipInputStream = null;
boolean status = true;

try {
inputStream = new FileInputStream(zipFilePath);

zipInputStream = new ZipInputStream(inputStream);
final byte[] data = new byte[1024];

while (true) {
ZipEntry zipEntry = null;
FileOutputStream outputStream = null;

try {
zipEntry = zipInputStream.getNextEntry();

if (zipEntry == null) break;

final String destination = Utils.getUserDataDirectory() + zipEntry.getName();

if (overwrite == false) {
if (Utils.isFileOrDirectoryExist(destination)) continue;
}

if (zipEntry.isDirectory())
{
Utils.createCompleteDirectoryHierarchyIfDoesNotExist(destination);
}
else
{
final File file = new File(destination);
// Ensure directory is there before we write the file.
Utils.createCompleteDirectoryHierarchyIfDoesNotExist(file.getParentFile());

int size = zipInputStream.read(data);

if (size > 0) {
outputStream = new FileOutputStream(destination);

do {
outputStream.write(data, 0, size);
size = zipInputStream.read(data);
} while(size >= 0);
}
}
}
catch (IOException exp) {
log.error(null, exp);
status = false;
break;
}
finally {
if (outputStream != null) {
try {
outputStream.close();
}
catch (IOException exp) {
log.error(null, exp);
break;
}
}

if (zipInputStream != null) {
try {
zipInputStream.closeEntry();
}
catch (IOException exp) {
log.error(null, exp);
break;
}
}
}

} // while(true)
}
catch (IOException exp) {
log.error(null, exp);
status = false;
}
finally {
if (zipInputStream != null) {
try {
zipInputStream.close();
} catch (IOException ex) {
log.error(null, ex);
}
}

if (inputStream != null) {
try {
inputStream.close();
} catch (IOException ex) {
log.error(null, ex);
}
}
}
return status;
}

“MyDirectory\MyFile.txt”而不是 MyFile.txt 被放置在文件夹 MyDirectory 下。

我尝试通过将 zip 文件创建代码更改为来解决问题

    String[] filenames = new String[]{"MyDirectory" + "/" + "MyFile.txt"};

但是,通过硬编码分隔符,这是一个合格的解决方案吗?它可以在 Mac OS 下运行吗? (我没有 Mac 可以试用)

最佳答案

是的,您的解决方案(虽然显然不够优雅)是正确的方法。“/”应该在 zipentry 内部使用,而不是本地的 File.separator

关于java - 在 Windows 中创建 Zip 文件并在 Linux 中提取 Zip 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2549766/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com