1

現在、TrueZipを使用して、MultiPartFileを介してサーバーにアップロードされたZipファイルにファイルを追加しています。

問題 ファイルを追加すると、zipが無効になります。zipファイルとして開くことはできなくなりました。

コード アップロードコントローラーの関連コードから始めましょう(ファイルはMultiPartFileです)。

    // Get the file
    File dest = null;
    TFile zip = null;
    try {
        // Obtain the file locally, zip, and delete the old
        dest = new File(request.getRealPath("") + "/datasource/uploads/" + fixedFileName);
        file.transferTo(dest);

        // Validate
        zip = new TFile(dest);
        resp = mls.validateMapLayer(zip);
        // Now perform the upload and delete the temp file
        FoundryUserDetails userDetails = (FoundryUserDetails) SecurityContextHolder.getContext().getAuthentication()
                .getPrincipal();
        UserIdentity ui = userDetails.getUserIdentity();

        MapLayer newLayer = new MapLayer();

                    // generate the prj
        mls.generateProjection(resp, dest.getAbsolutePath(), projection);

メソッド「generateProjection」は、ファイルが追加される場所です。

public void generateProjection(UploadMapResponse resp, String fLoc, FoundryCRS proj) throws NoSuchAuthorityCodeException,
        FactoryException, IOException {
    TFile projFile = new TFile(fLoc, resp.getLayerName() + ".prj");
    CoordinateReferenceSystem crs = CRS.decode(proj.getEpsg());
    String wkt = crs.toWKT();
    TConfig config = TConfig.push();
    try {
        config.setOutputPreferences(config.getOutputPreferences().set(FsOutputOption.GROW));
        TFileOutputStream writer = new TFileOutputStream(projFile);
        try {
            writer.write(wkt.getBytes());
        } finally {
            writer.close();
        }
    } finally {
        config.close();
    }
}

これがまったく機能するかどうかをテストするために、私は簡単なメインでそれを試しました:

public static void main(String[] args) {
    File f = new File("C:/Data/SierritaDec2011TopoContours.zip");
    TFile tf = new TFile(f);
    tf.listFiles();

    TFile proj = new TFile(f, "test.prj");
    TConfig config = TConfig.push();
    try {
        config.setOutputPreferences(config.getOutputPreferences().set(FsOutputOption.GROW));
        TFileOutputStream writer = null;
        try {
            writer = new TFileOutputStream(proj);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }

        try {
            writer.write("Hello Zip world".getBytes());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    } finally {
        // Pop the current configuration off the inheritable thread local
        // stack.
        config.close();
    }
}

もちろん、これは問題なく機能します。

質問

MultiPartFileがローカルファイルにコピーされているWebサーバーで、TFileOutputStreamが正しく書き込みに失敗する理由を誰かが知っていますか?

4

1 に答える 1

0

長時間実行されるサーバーアプリでは、アーカイブファイルを同期またはアンマウントするために、TVFS.sync()またはTVFS.umount()への呼び出しを追加する必要がある場合があります。ZIPファイルの場合、これにより、ZIPファイルの最後に中央ディレクトリが書き込まれます。これは、有効なZIPファイルを作成するために必要です。

Javadocをチェックして、ユースケースに最適な呼び出しを決定してください:http://truezip.java.net/apidocs/de/schlichtherle/truezip/file/TVFS.html

また、各追加操作の後にTFVS.sync()またはTVFS.umount()を呼び出すと、毎回書き込まれる中央ディレクトリが大きくなり、大きなオーバーヘッドが発生することに注意してください。したがって、これを正確に行う必要があるのはいつかを検討する価値があります。一般的に、これはサードパーティにZIPファイルへのアクセスを許可する場合にのみ必要です。サードパーティとは、ZIPファイルにアクセスするためにTrueZIPカーネルと対話していない人のことです。

于 2013-02-02T09:37:20.070 に答える