gpt4 book ai didi

java - 从 zip 创建文件 - android

转载 作者:行者123 更新时间:2023-12-01 15:15:25 25 4
gpt4 key购买 nike

我有一个文件夹/mnt/sdcard/Bluetooth,其中包含多个zip 文件。每个 zip 文件仅包含一个文件。如何将每个 zip 文件的内容提取到一个新文件中,其中包含每个 zip 文件的内容?这是我到目前为止所做的:

public class Main extends Activity {

Object[] arrayOfRarFiles;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

String path = "/mnt/sdcard/Bluetooth";
String nameOfFiles;
File folder = new File(path);
File[] listOfFiles = folder.listFiles();

for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
nameOfFiles = listOfFiles[i].getName();

if (nameOfFiles.endsWith(".zip")
|| nameOfFiles.endsWith(".ZIP")) {

try {
extractFile(nameOfFiles);
} catch (FileNotFoundException e) {
Log.e("EXTRACTFILE SAYS: ", e.getMessage());
}

}
}
}
}

public void extractFile(String path) throws FileNotFoundException {

String zipFileName = "/mnt/sdcard/Bluetooth/" + path;
String extractedFileName = getApplicationContext().getFilesDir()
.getPath().toString()
+ "Finger.FIR";

ZipInputStream inStream = new ZipInputStream(new FileInputStream(
zipFileName));
OutputStream outStream = new FileOutputStream(extractedFileName);





Toast.makeText(getApplicationContext(), zipFileName,
Toast.LENGTH_SHORT).show();

}

extractFile 方法中的最后一个 toast 输出每个 zip 文件的名称。 zip 文件夹内的文件是 .FIR 文件

最佳答案

我认为你可以使用我在另一个 SO question 中找到的以下函数。

注意根据需要正确设置路径和文件名参数。

public void extractFile(String path) throws FileNotFoundException {

String zipFileName = "/mnt/sdcard/Bluetooth/" + path;
String extractedFileName = getApplicationContext().getFilesDir()
.getPath().toString()
+ "Finger.FIR";

ZipInputStream inStream = new ZipInputStream(new FileInputStream(
zipFileName));
OutputStream outStream = new FileOutputStream(extractedFileName);

unpackZip(path ,zipFileName)



/*Toast.makeText(getApplicationContext(), zipFileName,
Toast.LENGTH_SHORT).show();*/

}

private boolean unpackZip(String path, String zipname)
{
InputStream is;
ZipInputStream zis;
try
{
String filename;
is = new FileInputStream(path + zipname);
zis = new ZipInputStream(new BufferedInputStream(is));
ZipEntry ze;
byte[] buffer = new byte[1024];
int count;

while ((ze = zis.getNextEntry()) != null)
{
// zapis do souboru
filename = ze.getName();
FileOutputStream fout = new FileOutputStream(path + filename);

// cteni zipu a zapis
while ((count = zis.read(buffer)) != -1)
{
fout.write(buffer, 0, count);
}

fout.close();
zis.closeEntry();
}

zis.close();
}
catch(IOException e)
{
e.printStackTrace();
return false;
}

return true;
}

关于java - 从 zip 创建文件 - android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11669861/

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