-3

これが Linux で動作しない理由を誰かが理解するのを手伝ってくれるかどうか疑問に思っていました。このアップデーターが対象とするゲームは、Mac、Linux、および Windows 用に作成されているため、誰もが問題なく使用できるようにしようとしています。

プログラムは、単一のエラーなしで Windows 上で完全に動作します。Linux を使用して必要なすべてのものを実行する友人がいて、フォルダーを検出して停止するだけです。

package mu;

import java.awt.Component;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;

public class Main extends JFrame
{
    private static final long serialVersionUID = 2627728992434582295L;
    public String site;
    public String filename;
    public static String[] mods = new String[99];
    public static String[] Cver = new String[99]; //Current Version
    public static String[] webs = new String[99]; //Web Address to file
    public static String[] Nver = new String[99]; //New Version
    public static int num = 0;
    public static final void main(String[] args) throws Exception
    {
        folders();
    }
    public static void folders(){
        File dir = new File("").getAbsoluteFile();
        File[] files = dir.listFiles();
        FileFilter filefilter= new FileFilter(){
            public boolean accept(File file){
                return file.isDirectory();
            }
        };
        files = dir.listFiles(filefilter);
        System.out.println(files.length + " Mods found!");
        if(files.length == 0){
        }else{
            for(int i=0; i<files.length; i++){
                File filename = files[i];
                Freader(filename);
            }
        }
    }
    public static void Freader(File dir){
        File f = new File(dir + "\\version.txt");
        if(f.exists()){
            try{
                FileReader in = new FileReader(f);
                BufferedReader br = new BufferedReader(in);
                String str;
                int line = 0;
                while((str = br.readLine()) != null){
                    if(line == 0){
                        mods[num] = str;
                        System.out.println(mods[num]);
                    }else if(line == 1){
                        Cver[num] = str;
                        System.out.println(Cver[num]);
                    }else if(line == 2){
                        webs[num] = str;
                        System.out.println(webs[num]);
                        Oreader(webs[num]);
                    }
                    line++;
                }
                num++;
                br.close();
            }catch(IOException e){
                e.printStackTrace();
            }
        }else{
        }
    }
    public static void Oreader(String dir){
        try{
            URL url = new URL(dir);
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String str;
            int line = 0;
            boolean newVer = false;
            while((str = in.readLine()) != null){
                if (line == 0){
                    Nver[num] = str;
                    if(!Cver[num].equals(Nver[num])){
                        System.out.println(Nver[num]);
                        System.out.println("New version available!");
                        newVer = true;
                    }
                }else if(newVer && line == 1){
                    DL(str, (mods[num]+Nver[num].toString() + ".zip"));
                }
                line++;
            }
            in.close();
        }catch(MalformedURLException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
    public static void DL(String web,String name){
        File dir = new File("").getAbsoluteFile();
        float Precent = 0;
        JFrame frm = new JFrame();
        JProgressBar current = new JProgressBar(0,100);
        current.setSize(394,25);
        current.setValue(0);
        current.setStringPainted(true);
        frm.setTitle("Mod Updater");
        frm.add(current);
        JLabel label1 = new JLabel(mods[num],JLabel.CENTER);
        frm.add(label1).setBounds(0, 10, 394, 50);
        frm.setSize(400,100);
        frm.setLayout(null);
        frm.setLocationRelativeTo(null);
        frm.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frm.setResizable(false);
        frm.setVisible(true);
        String site = web;
        try{
            URL url = new URL(site);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            int filesize = connection.getContentLength();
            float totalDataRead=0;
            java.io.BufferedInputStream in = new java.io.BufferedInputStream(connection.getInputStream());
            java.io.FileOutputStream fos = new java.io.FileOutputStream(name);
            java.io.BufferedOutputStream bout = new java.io.BufferedOutputStream(fos,1024);
            byte[] data = new byte[1024];
            int i=0;
            while((i=in.read(data,0,1024))>=0){
                totalDataRead=totalDataRead+i;
                bout.write(data,0,i);
                Precent=(totalDataRead*100)/filesize;
                current.setValue((int)Precent);
                if(Precent == 100){
                    extract(dir + "\\" +name);
                }
            }
            bout.close();
            in.close();
        }catch(Exception e){
            javax.swing.JOptionPane.showConfirmDialog((Component)null,e.getMessage(),"Error", javax.swing.JOptionPane.DEFAULT_OPTION);
        }
    }
    public static void extract(String filePath){
            File dir = new File("").getAbsoluteFile();
            FileInputStream fis = null;
            ZipInputStream zipIs = null;
            ZipEntry zEntry = null;
            boolean dirs = false;
            try {
                fis = new FileInputStream(filePath);
                zipIs = new ZipInputStream(new BufferedInputStream(fis));
                while((zEntry = zipIs.getNextEntry()) != null){
                    try{
                        byte[] tmp = new byte[4*1024];
                        FileOutputStream fos = null;
                        String opFilePath = dir +"/"+zEntry.getName();
                        if(zEntry.isDirectory()){
                            dirs = new File(zEntry.getName()).mkdirs();
                        }else{
                            System.out.println("Extracting file to "+opFilePath);
                            fos = new FileOutputStream(opFilePath);
                            int size = 0;
                            while((size = zipIs.read(tmp)) != -1){
                                    fos.write(tmp, 0 , size);
                            }
                        }
                        fos.flush();
                        fos.close();
                    } catch(Exception ex){
                        ex.printStackTrace();
                    }
                }
                zipIs.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        System.out.println("Finished");
    }
}
4

1 に答える 1

1

問題のある行はここにあると思います:

extract(dir + "\\" +name);

私が覚えている限り、Linux はそのパスのバックスラッシュを認識しません。一方、Windows はスラッシュを認識します。また、二重スラッシュは冗長です。代わりに、そのビットを「/」に置き換えてみてください。

さらに良いことに、@Teeg が提案したようFile.pathSeparator()に、真のクロスプラットフォーム ソリューションに使用します。彼からの引用は次のとおりです。

さらに、パスセパレーターをハードコーディングしないでください。 File.pathSeparator() を使用するか、さらに良い new File(somePath, filename) を使用してください

しかし、これは推測にすぎません。解決しようとしている問題について、より多くの情報が必要です。

編集:スラッシュが間違っている他のいくつかの場所を見つけました。すべてをリストするわけではありません。検索して置換します。

于 2013-09-23T20:51:38.943 に答える