On Sunday 23 November 2014 12:01:32 Olivier Chorier wrote:
> Hello everybody,
Hi Olivier,

> I'm looking for a way to repair thousands of corrupted odt files. Those
> files have been corrupted after FTP send operations, for some reason.
> 
> The "zip" structure seems corrupted. I tried using 7-zip to unzip and zip
> again the odt file, but it doesn't work every time.
> Libre Office can repair them every time, but I need to open, repair, save
> and close those files one by one.
> 
> I wonder if a script or something like that could be used to repair those
> files. I can't do it manually because there is thousand of them. Ideally, I
> could check them, and repair them automatically using a bash or a java
> script...
The zip utility (of Info-ZIP) has the -F and -FF flags that should be able to 
recover your files. Anyway, if you want to use LibreOffice for that, you could 
use the attached Java code as a starting point. The key point in this code is 
to explicitly set the input filter name, and the "RepairPackage" property to 
true in the input MediaDescriptor.

Maxim
import com.sun.star.beans.PropertyValue;
import com.sun.star.comp.helper.Bootstrap;
import com.sun.star.frame.*;
import com.sun.star.lang.*;
import com.sun.star.uno.*;
import com.sun.star.util.XCloseable;

public class DocRepair {

    public static void main(String[] args) {
        try {
            XComponentContext xContext = Bootstrap.bootstrap();
            XMultiComponentFactory xMCF = xContext.getServiceManager();
            Object oDesktop = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", xContext);

            // Open the corrupted document
            PropertyValue inputMediaDesc[] = new PropertyValue[3];

            inputMediaDesc[0] = new PropertyValue();
            inputMediaDesc[0].Name = "Hidden";
            inputMediaDesc[0].Value = Boolean.TRUE;

            inputMediaDesc[1] = new PropertyValue();
            inputMediaDesc[1].Name = "RepairPackage";
            inputMediaDesc[1].Value = Boolean.TRUE;

            inputMediaDesc[2] = new PropertyValue();
            inputMediaDesc[2].Name = "FilterName";
            inputMediaDesc[2].Value = "writer8";

            XComponentLoader xCLoader = UnoRuntime.queryInterface(XComponentLoader.class, oDesktop);
            XComponent xDocument = xCLoader.loadComponentFromURL("file:///home/maxim/Downloads/corrupted.odt", "_blank", 0, inputMediaDesc);

            // Save to a new file
            PropertyValue outputMediaDesc[] = new PropertyValue[2];

            outputMediaDesc[0] = new PropertyValue();
            outputMediaDesc[0].Name = "Overwrite";
            outputMediaDesc[0].Value = Boolean.TRUE;

            outputMediaDesc[1] = new PropertyValue();
            outputMediaDesc[1].Name = "FilterName";
            outputMediaDesc[1].Value = "writer8";

            XStorable xStorable = UnoRuntime.queryInterface(XStorable.class, xDocument);
            xStorable.storeAsURL("file:///home/maxim/Downloads/repaired.odt", outputMediaDesc);

            // Close the document
            XCloseable xCloseable = UnoRuntime.queryInterface(XCloseable.class, xStorable);
            if (xCloseable != null) {
                xCloseable.close(false);
            } else {
                XComponent xComp = UnoRuntime.queryInterface(XComponent.class, xStorable);
                xComp.dispose();
            }
            System.exit(0);
        } catch (java.lang.Exception e) {
            e.printStackTrace(System.err);
            System.exit(1);
        }
    }
}
_______________________________________________
LibreOffice mailing list
LibreOffice@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/libreoffice

Reply via email to