Pregunta

I am currently using TrueZip to add a file to a Zip file that was uploaded to a server via MultiPartFile.

The Problem Upon appending a file the zip becomes invalid. It can no longer be opened as a zip file.

The Code Let's start with the relevant code in my upload controller (file is the 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);

The method "generateProjection" is where the file is added:

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();
    }
}

In order to test if this worked at all I tried it in a simple main:

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();
    }
}

Which, of course, works just fine.

The Question

Does anyone have insight into why, in a web server with a MultiPartFile copied to a local file, the TFileOutputStream fails to write properly?

¿Fue útil?

Solución

In a long running server app, you may need to add a call to TVFS.sync() or TVFS.umount() in order to sync or umount archive files. In the case of ZIP files, this will trigger to write the Central Directory at the end of the ZIP file, which is required to form a valid ZIP file.

Please check the Javadoc to decide which call is the best for your use case: http://truezip.java.net/apidocs/de/schlichtherle/truezip/file/TVFS.html

Also, please note that calling TFVS.sync() or TVFS.umount() after each append operation will result in a growing Central Directory to be written each time, which results in huge overhead. So it's worth to consider when exactly you need to do this. Generally speaking this is only required when you want a third party to access the ZIP file. A third party is anyone not interacting with the TrueZIP Kernel for accessing the ZIP file.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top