里面的Zip文件,而不包括主文件夹在Android中Java的 [英] Zip files inside without including main folder In android java
问题描述
使用这个code将其完美地创造,比如我有文件夹的压缩文件即时
SD卡/音乐,当我创建xxx.zip文件,它会创建.zip文件包含在它的音乐/歌曲,还有一些子文件
但我想排除音乐中的.zip文件的主要文件夹,并希望它直接启动,包括它所有的歌曲和子文件夹,该怎么办呢 P>
我用这样的方式,但是当荏苒需要主文件夹名称和
公共无效unzipbutton(视图v){//按钮点击 尝试{
FOS的FileOutputStream =新的FileOutputStream(到/ mnt / SD卡/ songs.zip);
ZipOutputStream ZOS =新ZipOutputStream(FOS);
addDirToZipArchive(ZOS,新的文件(到/ mnt / SD卡/音乐),NULL);
zos.flush();
fos.flush();
zos.close();
fos.close();
}赶上(例外五){ e.printStackTrace();
} } 公共静态无效addDirToZipArchive(ZipOutputStream隔离区,文件fileToZip,字符串parrentDirectoryName)抛出异常{
如果(fileToZip == NULL ||!fileToZip.exists()){
返回;
} 串zipEntryName = fileToZip.getName();
如果(parrentDirectoryName =空&放大器;!&放大器;!parrentDirectoryName.isEmpty()){
zipEntryName = parrentDirectoryName +/+ fileToZip.getName();
} 如果(fileToZip.isDirectory()){
的System.out.println(++ zipEntryName);
对于(文件文件:fileToZip.listFiles()){
addDirToZipArchive(ZOS,文件zipEntryName);
}
}其他{
的System.out.println(+ zipEntryName);
字节[]缓冲区=新的字节[1024];
FIS的FileInputStream =新的FileInputStream(fileToZip);
zos.putNextEntry(新的ZipEntry(zipEntryName));
INT长;
而((长度= fis.read(缓冲液))大于0){
zos.write(缓冲液,0,长度);
}
zos.closeEntry();
fis.close();
}
}
在这个任何人的帮助,谢谢
这一直是如此棘手至今没有办法工作在时刻对我来说:/
在您的ZipEntry检查zipEntryName的完整路径,它必须是这样的:音乐/粉红/ try.mp3只调用一个子,以获得作为zipEntryName粉红/ try.mp3,你的zip文件将是相同的songs.zip,但在它的内部,你会发现文件夹红粉,而不是主文件夹音乐/粉红......
这为我工作。
im using this code it perfectly create zip files for example i have folder
sdcard/music and when i create xxx.zip file it creates .zip file includes music/songs and also some sub folders in it
but i want to exclude "music" the main Folder in the .zip file and want it to start directly including all songs and subfolders in it,how to do it
i use this way but it takes main folder name as well when zipping
public void unzipbutton(View v){ // button click
try {
FileOutputStream fos = new FileOutputStream("/mnt/sdcard/songs.zip");
ZipOutputStream zos = new ZipOutputStream(fos);
addDirToZipArchive(zos, new File("/mnt/sdcard/music"), null);
zos.flush();
fos.flush();
zos.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void addDirToZipArchive(ZipOutputStream zos, File fileToZip, String parrentDirectoryName) throws Exception {
if (fileToZip == null || !fileToZip.exists()) {
return;
}
String zipEntryName = fileToZip.getName();
if (parrentDirectoryName!=null && !parrentDirectoryName.isEmpty()) {
zipEntryName = parrentDirectoryName + "/" + fileToZip.getName();
}
if (fileToZip.isDirectory()) {
System.out.println("+" + zipEntryName);
for (File file : fileToZip.listFiles()) {
addDirToZipArchive(zos, file, zipEntryName);
}
} else {
System.out.println(" " + zipEntryName);
byte[] buffer = new byte[1024];
FileInputStream fis = new FileInputStream(fileToZip);
zos.putNextEntry(new ZipEntry(zipEntryName));
int length;
while ((length = fis.read(buffer)) > 0) {
zos.write(buffer, 0, length);
}
zos.closeEntry();
fis.close();
}
}
anybody help on this,thanks
this has been so tricky till now no way works at the moments for me :/
In your ZipEntry check the full path of zipEntryName, it must be something like: "music/Pink/try.mp3" just invoke a substring in order to obtain as zipEntryName "Pink/try.mp3", your zip file will be the same "songs.zip" but inside of it you'll find the folder "Pink" instead the main folder "music/Pink..."
It worked for me.
这篇关于里面的Zip文件,而不包括主文件夹在Android中Java的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!