001/* 002 * Copyright 2007-2018 The jdeb developers. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.vafer.jdeb.producers; 017 018import java.io.File; 019import java.io.FileInputStream; 020import java.io.IOException; 021 022import org.apache.commons.compress.archivers.tar.TarArchiveEntry; 023import org.vafer.jdeb.DataConsumer; 024import org.vafer.jdeb.DataProducer; 025import org.vafer.jdeb.mapping.Mapper; 026 027/** 028 * DataProducer representing a single file 029 * For cross-platform permissions and ownerships you probably want to use a Mapper, too. 030 */ 031public final class DataProducerFile extends AbstractDataProducer implements DataProducer { 032 033 private final File file; 034 035 private final String destinationName; 036 037 public DataProducerFile( final File pFile, String pDestinationName, String[] pIncludes, String[] pExcludes, Mapper[] pMapper ) { 038 super(pIncludes, pExcludes, pMapper); 039 file = pFile; 040 destinationName = pDestinationName; 041 } 042 043 public void produce( final DataConsumer pReceiver ) throws IOException { 044 String fileName; 045 if (destinationName != null && destinationName.trim().length() > 0) { 046 fileName = destinationName.trim(); 047 } else { 048 fileName = file.getName(); 049 } 050 051 TarArchiveEntry entry = Producers.defaultFileEntryWithName(fileName); 052 053 entry = map(entry); 054 055 entry.setSize(file.length()); 056 057 Producers.produceInputStreamWithEntry(pReceiver, new FileInputStream(file), entry); 058 } 059 060}