⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 copycompare.java

📁 java 读写word excel ppt
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* ====================================================================   Licensed to the Apache Software Foundation (ASF) under one or more   contributor license agreements.  See the NOTICE file distributed with   this work for additional information regarding copyright ownership.   The ASF licenses this file to You under the Apache License, Version 2.0   (the "License"); you may not use this file except in compliance with   the License.  You may obtain a copy of the License at       http://www.apache.org/licenses/LICENSE-2.0   Unless required by applicable law or agreed to in writing, software   distributed under the License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   See the License for the specific language governing permissions and   limitations under the License.==================================================================== */        package org.apache.poi.hpsf.examples;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import org.apache.poi.hpsf.HPSFRuntimeException;import org.apache.poi.hpsf.MarkUnsupportedException;import org.apache.poi.hpsf.MutablePropertySet;import org.apache.poi.hpsf.NoPropertySetStreamException;import org.apache.poi.hpsf.PropertySet;import org.apache.poi.hpsf.PropertySetFactory;import org.apache.poi.hpsf.Util;import org.apache.poi.hpsf.WritingNotSupportedException;import org.apache.poi.poifs.eventfilesystem.POIFSReader;import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent;import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener;import org.apache.poi.poifs.filesystem.DirectoryEntry;import org.apache.poi.poifs.filesystem.DocumentEntry;import org.apache.poi.poifs.filesystem.DocumentInputStream;import org.apache.poi.poifs.filesystem.Entry;import org.apache.poi.poifs.filesystem.POIFSDocumentPath;import org.apache.poi.poifs.filesystem.POIFSFileSystem;import org.apache.poi.util.TempFile;/** * <p>This class copies a POI file system to a new file and compares the copy * with the original.</p> *  * <p>Property set streams are copied logically, i.e. the application * establishes a {@link org.apache.poi.hpsf.PropertySet} of an original property * set, creates a {@link org.apache.poi.hpsf.MutablePropertySet} from the * {@link org.apache.poi.hpsf.PropertySet} and writes the * {@link org.apache.poi.hpsf.MutablePropertySet} to the destination POI file * system. - Streams which are no property set streams are copied bit by * bit.</p> *  * <p>The comparison of the POI file systems is done logically. That means that * the two disk files containing the POI file systems do not need to be * exactly identical. However, both POI file systems must contain the same * files, and most of these files must be bitwise identical. Property set * streams, however, are compared logically: they must have the same sections * with the same attributs, and the sections must contain the same properties. * Details like the ordering of the properties do not matter.</p> * * @author Rainer Klute <a * href="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a> * @version $Id: CopyCompare.java 489730 2006-12-22 19:18:16Z bayard $ * @since 2003-09-19 */public class CopyCompare{    /**     * <p>Runs the example program. The application expects one or two     * arguments:</p>     *      * <ol>     *      * <li><p>The first argument is the disk file name of the POI filesystem to     * copy.</p></li>     *      * <li><p>The second argument is optional. If it is given, it is the name of     * a disk file the copy of the POI filesystem will be written to. If it is     * not given, the copy will be written to a temporary file which will be     * deleted at the end of the program.</p></li>     *      * </ol>     *     * @param args Command-line arguments.     * @exception MarkUnsupportedException if a POI document stream does not     * support the mark() operation.     * @exception NoPropertySetStreamException if the application tries to     * create a property set from a POI document stream that is not a property     * set stream.     * @exception IOException if any I/O exception occurs.     * @exception UnsupportedEncodingException if a character encoding is not     * supported.     */    public static void main(final String[] args)    throws NoPropertySetStreamException, MarkUnsupportedException,           UnsupportedEncodingException, IOException    {        String originalFileName = null;        String copyFileName = null;        /* Check the command-line arguments. */        if (args.length == 1)        {            originalFileName = args[0];            File f = TempFile.createTempFile("CopyOfPOIFileSystem-", ".ole2");            f.deleteOnExit();            copyFileName = f.getAbsolutePath();        }        else if (args.length == 2)        {            originalFileName = args[0];            copyFileName = args[1];        }        else        {            System.err.println("Usage: " + CopyCompare.class.getName() +                               "originPOIFS [copyPOIFS]");            System.exit(1);        }        /* Read the origin POIFS using the eventing API. The real work is done         * in the class CopyFile which is registered here as a POIFSReader. */        final POIFSReader r = new POIFSReader();        final CopyFile cf = new CopyFile(copyFileName);        r.registerListener(cf);        r.read(new FileInputStream(originalFileName));                /* Write the new POIFS to disk. */        cf.close();        /* Read all documents from the original POI file system and compare them         * with the equivalent document from the copy. */        final POIFSFileSystem opfs =            new POIFSFileSystem(new FileInputStream(originalFileName));        final POIFSFileSystem cpfs =            new POIFSFileSystem(new FileInputStream(copyFileName));        final DirectoryEntry oRoot = opfs.getRoot();        final DirectoryEntry cRoot = cpfs.getRoot();        final StringBuffer messages = new StringBuffer();        if (equal(oRoot, cRoot, messages))            System.out.println("Equal");        else            System.out.println("Not equal: " + messages.toString());    }    /**     * <p>Compares two {@link DirectoryEntry} instances of a POI file system.     * The directories must contain the same streams with the same names and     * contents.</p>     *     * @param d1 The first directory.     * @param d2 The second directory.     * @param msg The method may append human-readable comparison messages to     * this string buffer.      * @return <code>true</code> if the directories are equal, else     * <code>false</code>.     * @exception MarkUnsupportedException if a POI document stream does not     * support the mark() operation.     * @exception NoPropertySetStreamException if the application tries to     * create a property set from a POI document stream that is not a property     * set stream.     * @throws UnsupportedEncodingException      * @exception IOException if any I/O exception occurs.     */    private static boolean equal(final DirectoryEntry d1,                                 final DirectoryEntry d2,                                 final StringBuffer msg)    throws NoPropertySetStreamException, MarkUnsupportedException,           UnsupportedEncodingException, IOException    {        boolean equal = true;        /* Iterate over d1 and compare each entry with its counterpart in d2. */        for (final Iterator i = d1.getEntries(); equal && i.hasNext();)        {            final Entry e1 = (Entry) i.next();            final String n1 = e1.getName();            Entry e2 = null;            try            {                e2 = d2.getEntry(n1);            }            catch (FileNotFoundException ex)            {                msg.append("Document \"" + e1 + "\" exists, document \"" +                           e2 + "\" does not.\n");                equal = false;                break;            }            if (e1.isDirectoryEntry() && e2.isDirectoryEntry())                equal = equal((DirectoryEntry) e1, (DirectoryEntry) e2, msg);            else if (e1.isDocumentEntry() && e2.isDocumentEntry())                equal = equal((DocumentEntry) e1, (DocumentEntry) e2, msg);            else            {                msg.append("One of \"" + e1 + "\" and \"" + e2 + "\" is a " +                           "document while the other one is a directory.\n");                equal = false;            }        }        /* Iterate over d2 just to make sure that there are no entries in d2         * that are not in d1. */        for (final Iterator i = d2.getEntries(); equal && i.hasNext();)        {            final Entry e2 = (Entry) i.next();            final String n2 = e2.getName();            Entry e1 = null;            try            {                e1 = d1.getEntry(n2);            }            catch (FileNotFoundException ex)            {                msg.append("Document \"" + e2 + "\" exitsts, document \"" +                           e1 + "\" does not.\n");                equal = false;                break;            }        }        return equal;    }    /**     * <p>Compares two {@link DocumentEntry} instances of a POI file system.     * Documents that are not property set streams must be bitwise identical.     * Property set streams must be logically equal.</p>     *     * @param d1 The first document.     * @param d2 The second document.     * @param msg The method may append human-readable comparison messages to     * this string buffer.      * @return <code>true</code> if the documents are equal, else     * <code>false</code>.     * @exception MarkUnsupportedException if a POI document stream does not     * support the mark() operation.     * @exception NoPropertySetStreamException if the application tries to     * create a property set from a POI document stream that is not a property     * set stream.     * @throws UnsupportedEncodingException      * @exception IOException if any I/O exception occurs.     */    private static boolean equal(final DocumentEntry d1, final DocumentEntry d2,                                 final StringBuffer msg)    throws NoPropertySetStreamException, MarkUnsupportedException,           UnsupportedEncodingException, IOException    {        boolean equal = true;        final DocumentInputStream dis1 = new DocumentInputStream(d1);        final DocumentInputStream dis2 = new DocumentInputStream(d2);        if (PropertySet.isPropertySetStream(dis1) &&            PropertySet.isPropertySetStream(dis2))        {            final PropertySet ps1 = PropertySetFactory.create(dis1);            final PropertySet ps2 = PropertySetFactory.create(dis2);            equal = ps1.equals(ps2);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -