zip4j如何提取目录

时间:2013-10-12 01:31:14

标签: java zip4j

我需要使用zip4j从zip中提取目录。我可以在目录中找到每个文件并将其解压缩。

如何列出目录中的文件?

或者,是否有一个实用程序将目录提取到路径?

4 个答案:

答案 0 :(得分:1)

从Zip4j的ZipFile,您可以获取此zip文件中所有文件头的列表。然后你可以从这个文件头检查,如果这个“文件”是一个目录。如果是,请提取它。

以下是仅从zip文件中提取目录的示例代码

import java.util.List;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.FileHeader;

public class ExtractDirectory {

    public static void main(String[] args) {
        try {
            ZipFile zipFile = new ZipFile("myZipWithDirectories.zip");
            if (zipFile.isEncrypted()) {
                zipFile.setPassword("test".toCharArray());
            }

            @SuppressWarnings("unchecked")
            List<FileHeader> fileHeaders = zipFile.getFileHeaders();

            for(FileHeader fileHeader : fileHeaders) {
                if (fileHeader.isDirectory()) {
                    zipFile.extractFile(fileHeader, "anyValidPathToExtractTo");
                }
                //Alternatively, if you want to extract a directory by its name
                //if (fileHeader.isDirectory() && fileHeader.getFileName().equals("myDirectoryName")) {
                //  zipFile.extractFile(fileHeader, "anyValidPathToExtractTo");
                //}
            }

        } catch (ZipException e) {
            e.printStackTrace();
        }
    }
}

答案 1 :(得分:0)

在zip4j中,您应该能够利用myZipFile.getFileHeaders()zipFile.getFileHeader("TargetFolder");功能来提取目标文件夹。

//List<FileHeader> fHeaders = myZipFile.getFileHeaders(); for all file Header

FileHeader targetFileHeader = zipFile.getFileHeader("TargetFolder"); 

if (targetFileHeader.isDirectory()) {
      File f = new File("anyGivenDirectory/" + targetFileHeader.getFileName());
    f.mkdirs();  // mkdirs() is different from mkdir()
    zipFile.extractFile(fileHeader, f.toString());
   }

据我所知,另一个已知的好图书馆:zt-zip。但我不确定它支持解密。

答案 2 :(得分:0)

使用此库www.lingala.net/zip4j /

将此jar文件添加到app的lib文件夹中。

检查您的导入

import net.lingala.zip4j.core.ZipFile;

import net.lingala.zip4j.exception.ZipException;

import net.lingala.zip4j.model.FileHeader;

使用以下方法

解压缩( “/ SD卡/ file.zip”, “/ SD卡/ unzipFolder”)


 public static void unzip(String Filepath, String DestinationFolderPath) {

         try {
            ZipFile zipFile = new ZipFile(Filepath);
            List fileHeaders = zipFile.getFileHeaders();
            for(FileHeader fileHeader : fileHeaders) {
                String fileName = fileHeader.getFileName();

                if (fileName.contains("\\")) {
                    fileName=fileName.replace("\\","\\\\");
                    String[] Folders=fileName.split("\\\\");
                    StringBuilder newFilepath=new StringBuilder();
                    newFilepath.append(DestinationFolderPath);
                    for (int i=0;i< Folders.length-1;i++){
                        newFilepath.append(File.separator);
                        newFilepath.append(Folders[i]);
                    }
                    zipFile.extractFile(fileHeader, newFilepath.toString(),null,Folders[Folders.length-1]);
                }else {
                    zipFile.extractFile(fileHeader,DestinationFolderPath);
                }
            }

        } catch (ZipException e) {
            e.printStackTrace();
        }
    }



答案 3 :(得分:0)

使用 const MainNavigator = createStackNavigator( { Home: HomeScreen, Categories: CategoriesScreen, Recipe: RecipeScreen, RecipesList: RecipesListScreen, Ingredient: IngredientScreen, Search: SearchScreen, IngredientsDetails: IngredientsDetailsScreen, Orders:OrdersScreen, SingleOrder:SingleOrder, OrderDetails:OrderDetailsScreen, Register:RegisterScreen, Edit:EditScreen, EditScreenSingle:SingleProductEditScreen }, { initialRouteName: 'Home', //headerMode: 'float', defaulfNavigationOptions: ({ navigation }) => ({ headerTitleStyle: { fontWeight: 'bold', textAlign: 'center', flex: 1, }, }) } ); const DrawerStack = createDrawerNavigator( { Main: MainNavigator }, { drawerPosition: 'left', initialRouteName: 'Main', drawerWidth: 250, contentComponent: DrawerContainer } ); export const AppContainer = createAppContainer(DrawerStack);方法提取文件和目录。如果要手动执行此操作,请尝试以下操作:

    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.1/umd/react.production.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.1/umd/react-dom.production.min.js"></script>