首页 文章

无法使用zip4j在java中创建zip文件

提问于
浏览
0

我使用java创建一个zip文件,但我无法获得任何代码 . 我尝试了更多特定于java 7和java 8的方法,但是它们都显示出某种错误,我的代码中是否有错误或需要改进?

File destination = null;
            JFileChooser chooser1 = new JFileChooser();
            chooser1.setDialogTitle("Select a directory with APK file contents");
            chooser1.setCurrentDirectory(new java.io.File("."));
            chooser1.setAcceptAllFileFilterUsed(false);
            chooser1.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int returnVal = chooser1.showOpenDialog(getParent());
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                destination = chooser1.getSelectedFile();
                JFileChooser chooser2 = new JFileChooser();
                FileNameExtensionFilter filter = new FileNameExtensionFilter("APK Files", "apk");
                chooser2.setFileFilter(filter);
                chooser2.setDialogTitle("Save APK as");
                int userSelection = chooser2.showSaveDialog(parentFrame);
                if (userSelection == JFileChooser.APPROVE_OPTION) {
                    File apksavedestination = chooser2.getSelectedFile();
                    String apkname = chooser2.getSelectedFile().getName() + ".apk";
                    String zipFile = apksavedestination + apkname;
                    String zipfileSource = destination.getAbsolutePath();

                    try {
                        ZipFile zipFileAtLast = new ZipFile(zipFile);
                        ArrayList<File> inFolder = new ArrayList<File>();
                        inFolder.add(new File(destination.getAbsolutePath()));
                        ZipParameters parameters = new ZipParameters();
                        parameters.setIncludeRootFolder(false);
                        parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
                        parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
                        zipFileAtLast.addFolder(inFolder, parameters);
                    } catch (ZipException e1) {
                        e1.printStackTrace();
                    }

我在这一行收到错误 zipFileAtLast.addFolder(inFolder, parameters);

请指导我:)

错误而运行时:异常在线程 “AWT-EventQueue的-0” java.lang.Error的:未解决问题汇编:该方法addFolder(ArrayList中,ZipParameters)是未定义的类型的ZipFile在dpcs.Interface $ 3.actionPerformed(Interface.java :143)在javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)在javax.swing.AbstractButton中$ Handler.actionPerformed(AbstractButton.java:2348)在javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)在javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)在java.awt.Component.processMouseEvent(Component.java:6535)在javax.swing.JComponent.processMouseEvent(JComponent.java:3324)位于java.awt.Compad上的java.awt.Component.processEvent(Component.java:6300),java.awt.Component上的java.awt.Container.processEvent(Container.java:2236) java.awt.Container.dispatchEventImpl中的.dispatchEventImpl(Component.java:4891)(容器) r.java:2294)java.awt.Light上的java.awt.Component.dispatchEvent(Component.java:4713)java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525) )java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)at java.awt.Container.dispatchEventImpl(Container.java:2280)at java.awt.Window.dispatchEventImpl(Window.java:2750)at java.awt .Component.dispatchEvent(Component.java:4713)位于java.awt.EventQueue.access上的java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758),java.awt.EventQueue $ 3中的$ 500(EventQueue.java:97) . 在java.security.ProtectionDomain的Java.security.AccessController.doPrivileged(Native Method)java.awt.EventQueue $ 3.run(EventQueue.java:703)上运行(EventQueue.java:709)$ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java) :76)at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)at java.awt.EventQueue $ 4.run(EventQueue.java:731)在java.awt.EventQueue中的$ 4.run(EventQueue.java:729)在java.security.AccessController.doPrivileged(本机方法)在java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)在java.awt中 . 位于java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)的java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)中的EventQueue.dispatchEvent(EventQueue.java:728),位于java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread)的.java:105)处于java.awt.EventDispatchThread.run java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)(EventDispatchThread.java:82)

2 回答

  • 1

    希望这些链接有所帮助

    如何用Java创建一个zip文件https://www.cis.upenn.edu/~bcpierce/courses/629/jdkdocs/api/java.util.zip.ZipFile.html http://www.programcreek.com /java-api-examples/java.util.zip.ZipFile如何在特定文件夹中添加文件(在ZIP中)http://www.programcreek.com/java-api-examples/index.php?class= net.lingala.zip4j.core.ZipFile&方法= addFolder

  • 0

    我知道了 . 问题是,所有解决方案都需要先将File对象转换为字符串!

相关问题